/// <summary> /// 示範List的Add可加入衍生類別 /// </summary> private static void ListAddInheritObjectExample() { Customer customer1 = new Customer() { ID = 101, Name = "Mark", Salary = 5000 }; SavingCustomer saving_customer1 = new SavingCustomer() { ID = 110, Name = "Pam", Salary = 6500 }; List <Customer> customers = new List <Customer>(); customers.Add(customer1); customers.Add(saving_customer1); Console.WriteLine("ListAddInheritObjectExample"); foreach (Customer c in customers) { Console.WriteLine("ID:{0}, Name:{1}, Salary:{2}", c.ID, c.Name, c.Salary); } Console.WriteLine("\n"); }
static void Main(string[] args) { Customer customer1 = new Customer() { ID = 101, Name = "Mark", Salary = 5000 }; Customer customer2 = new Customer() { ID = 102, Name = "honn", Salary = 5500 }; Customer customer3 = new Customer() { ID = 103, Name = "john", Salary = 3400 }; //Customer[] customers = new Customer[2]; //customers[0] = customer1; //customers[1] = customer2; //customers[2] = customer3;//error List <Customer> customers = new List <Customer>();//strong typed customers.Add(customer1); customers.Add(customer2); customers.Add(customer3); SavingCustomer sc = new SavingCustomer(); customers.Add(sc);//ok inheritance customers.Insert(0, customer3); Console.WriteLine(customers.IndexOf(customer3)); //0 Console.WriteLine(customers.IndexOf(customer3, 1)); //3 //start from 1, Console.WriteLine(customers.IndexOf(customer3, 1, 2)); //-1//start from 1,search 2 obj Console.WriteLine(customers.IndexOf(customer3, 1, 212)); //error Customer C = customers[0]; Console.WriteLine(C.Name); foreach (Customer Cc in customers) { Console.WriteLine(Cc.Name); } for (int i = 0; i < customers.Count; i++) { Customer c = customers[i]; Console.WriteLine(c.Name); } }
static void Main(string[] args) { SavingCustomer SC = new SavingCustomer(); Console.WriteLine(SC.Id); CorprateCustomer CC = new CorprateCustomer(); Console.WriteLine(CC.Id); }
public static void Main(string[] args) { SavingCustomer sc = new SavingCustomer(); CorporateCustomer cc = new CorporateCustomer(); }