static void Main(string[] args) { //概念:让一个对象能够表现出多种状态(类型) Chinese cn1 = new Chinese("韩梅梅"); Chinese cn2 = new Chinese("李雷"); Japanese j1 = new Japanese("树下君"); Japanese j2 = new Japanese("井边子"); Korea k1 = new Korea("金秀贤"); Korea k2 = new Korea("金贤秀"); American a1 = new American("科比布莱恩特"); American a2 = new American("奥尼尔"); Person[] pers = { cn1, cn2, j1, j2, k1, k2, a1, a2 }; for (int i = 0; i < pers.Length; i++) { pers[i].SayHellow(); } Console.ReadKey(); }
static void Main(string[] args) { //概念:让一个对象能够表现出多种的状态(类型) //实现多态的3种手段:1、虚方法 2、抽象类 3、接口 Chinese cn1 = new Chinese("韩梅梅"); Chinese cn2 = new Chinese("李雷"); Japanese j1 = new Japanese("树下君"); Japanese j2 = new Japanese("井边子"); Korea k1 = new Korea("金秀贤"); Korea k2 = new Korea("金贤秀"); American a1 = new American("科比布莱恩特"); American a2 = new American("奥尼尔"); Person[] pers = { cn1, cn2, j1, j2, k1, k2, a1, a2, new English("格林"), new English("玛利亚") }; for (int i = 0; i < pers.Length; i++) { //if (pers[i] is Chinese) //{ // ((Chinese)pers[i]).SayHello(); //} //else if (pers[i] is Japanese) //{ // ((Japanese)pers[i]).SayHello(); //} //else if (pers[i] is Korea) //{ // ((Korea)pers[i]).SayHello(); //} //else //{ // ((American)pers[i]).SayHello(); //} pers[i].SayHello(); } Console.ReadKey(); }