static void Main(string[] args) { //创建10个对象通过一个循环去调用他们各自打招呼的方法 Person[] pers = new Person[10]; Random r = new Random(); for (int i = 0; i < pers.Length; i++) { int rNumber = r.Next(1, 7); switch (rNumber)//1-6; { case 1:pers[i]=new Student(); break; case 2: pers[i] = new Teacher(); break; case 3: pers[i] = new ShuaiGuo(); break; case 4: pers[i] = new MeiLv(); break; case 5: pers[i] = new YeShou(); break; case 6: pers[i] = new Person(); break; } } for (int i = 0; i < pers.Length; i++) { //pers[i].PersonSayHi(); if (pers[i] is Student) { ((Student)pers[i]).StudentSayHi(); } else if (pers[i] is Teacher) { ((Teacher)pers[i]).TeacherSayHi(); } else if (pers[i] is ShuaiGuo) { ((ShuaiGuo)pers[i]).ShuaiGuoSayHi(); }else if(pers[i] is YeShou){ ((YeShou)pers[i]).YeShouSayHi(); }else if(pers[i] is MeiLv){ ((MeiLv)pers[i]).MeiLvSayHi(); }else{ pers[i].PersonSayHi(); } } Console.ReadKey(); }
static void Main(string[] args) { Random rm = new Random(); Persion[] pers = new Persion[10]; for (int i = 0; i < pers.Length; i++) { int rNumber = rm.Next(1, 7); switch (rNumber) { case 1: pers[i] = new Student(); break; case 2: pers[i] = new Teacher(); break; case 3: pers[i] = new Meinv(); break; case 4: pers[i] = new ShuaiGuo(); break; case 5: pers[i] = new YeShou(); break; case 6: pers[i] = new Persion(); break; } } for (int i = 0; i < pers.Length; i++) { ZhuanHuan(pers[i]); } Console.ReadKey(); }
static void Main(string[] args) { //创建10个对象通过一个循环去调用他们各自打招呼的方法 Person[] pers = new Person[10]; Random r = new Random(); for (int i = 0; i < pers.Length; i++) { int rNumber = r.Next(1, 7); switch (rNumber)//1-6; { case 1: pers[i] = new Student(); break; case 2: pers[i] = new Teacher(); break; case 3: pers[i] = new ShuaiGuo(); break; case 4: pers[i] = new MeiLv(); break; case 5: pers[i] = new YeShou(); break; case 6: pers[i] = new Person(); break; } } for (int i = 0; i < pers.Length; i++) { //pers[i].PersonSayHi(); if (pers[i] is Student) { ((Student)pers[i]).StudentSayHi(); } else if (pers[i] is Teacher) { ((Teacher)pers[i]).TeacherSayHi(); } else if (pers[i] is ShuaiGuo) { ((ShuaiGuo)pers[i]).ShuaiGuoSayHi(); } else if (pers[i] is YeShou) { ((YeShou)pers[i]).YeShouSayHi(); } else if (pers[i] is MeiLv) { ((MeiLv)pers[i]).MeiLvSayHi(); } else { pers[i].PersonSayHi(); } } Console.ReadKey(); }