【c#】教材page77專案實訓 和課堂要求的作業
阿新 • • 發佈:2019-02-14
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Student { public string name; //欄位 名字 public int age; //欄位 年齡 public int _class; //欄位 班級 public long number; //欄位 學號 public Student() //構造方法一 { this.name = "漲三"; //賦預設值 this.age = 11; this._class = 2017; this.number = 0000; } public Student(string F2Name, int F2Age, int F2_class, long F2Number) //構造方法二 { this.name = F2Name; //實參賦值給相應欄位 this.age = F2Age; this._class = F2_class; this.number = F2Number; } } class Program { static void Main(string[] args) { long sum = 0; const int SIZE = 5; Student[] a = new Student[SIZE]; //類陣列 for (int i = 0; i < SIZE; i++) { a[i] = new Student(); //陣列宣告 Console.WriteLine("第{0}次輸入資料:\n名字", i + 1); a[i].name=Console.ReadLine(); //陣列例項化 Console.WriteLine("年齡"); a[i].age= int.Parse(Console.ReadLine()); Console.WriteLine("班級"); a[i]._class= int.Parse(Console.ReadLine()); Console.WriteLine("學號"); a[i].number= long.Parse(Console.ReadLine()); Student c = new Student(a[i].name, a[i].age, a[i]._class, a[i].number); } for (int i=0;i<SIZE;i++) { sum += a[i].age; //呼叫10個不同物件的age } Console.WriteLine(sum/SIZE); Console.Read(); } } }
c#課堂作業:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace work { public class WaiMai { private long number; public long Number //號碼 { get { return number; } set { number = value; } } private string name; //姓名 public string Name { get { return name; } set { name = value; } } private string address; //地址 public string Address { get { return address; } set { address = value; } } public WaiMai() { } public WaiMai(long number) //自定義構造方法 { this.number = number; } public WaiMai(string name, string adress) { this.name = name; this.address = address; } public WaiMai(long number, string name, string address) { this.number = number; this.name = name; this.address = address; } public void show(long number) //普通方法 { Console.WriteLine("電話:" + number); } public void show(string name, string address) { Console.WriteLine("姓名:" + name + "\n地址:" + address); } public void show(long number, string name, string address) { Console.WriteLine("電話:{0}\n姓名:{1}\n地址:{2}", number, name, address); } } public class WaiMai2:WaiMai //派生類 { private long ordernumber; //訂單號 public long Ordernumber { get { return ordernumber; } set { ordernumber = value; } } new public long Number=13320180000; //成員隱藏 new public void show(long ordernumber,string name,string address) //派生類show方法重寫 { Console.WriteLine("訂單號:{0}\n姓名: {1}\n電話:"+Number+" \n地址:{2}",ordernumber, name, address); } public WaiMai2() { } public WaiMai2(long number, string name, string address) : base(number, name, address) //子類呼叫父類構造方法 { Console.WriteLine("\n\n子類呼叫父類構造方法\n電話:{0}\n姓名:{1}\n地址:{2}", Number, Name, Address); } } public class price //靜態類 { public static int giveprice=2; //靜態欄位 配送價格 public static void pricemain(int p) //靜態方法 引數為外賣價格 { p = giveprice + p; Console.WriteLine("價格:{0}元(含配送{1}元)",p,giveprice); } } class Program { static void Main(string[] args) { Console.WriteLine("訂單一:\n"); WaiMai w1 = new WaiMai(); w1.Number = 18888888888; w1.Name = "張三"; w1.Address = "重慶電子工程職業學院"; w1.show(w1.Number); w1.show(w1.Name,w1.Address); price.pricemain(13); Console.WriteLine("\n訂單二:\n"); WaiMai w2 = new WaiMai(); w2.Number = 13300000000; w2.Name = "李四"; w2.Address = "電子校"; w2.show(w2.Number,w2.Name,w2.Address); price.pricemain(8); Console.WriteLine("\n\n訂單三(派生類):\n"); WaiMai2 w4 = new WaiMai2(); w4.Ordernumber = 201080000; w4.show(w4.Ordernumber,"吳名","重慶電子校北區"); WaiMai2 w3 = new WaiMai2(18855555555, "李五", "重慶電子校南區"); // Console.WriteLine("\n\n電話:{0}\n姓名:{1}\n地址:{2}", w3.Number, w3.Name, w3.Address); Console.Read(); } } }