Esempio n. 1
0
        static void Main(string[] args)
        {
            Person p  = new Chinese("张三");
            Person p1 = new Korean("赵仁焕");
            Person p2 = new Americans("Mr.One");

            Person[] per = { p, p1, p2, new Chinese("张飞"), new Americans("Mr.Two") };
            for (int i = 0; i < per.Length; i++)
            {
                // 不用虚方法的话
                if (per[i] is Chinese)
                {
                    ((Chinese)per[i]).SayHello();
                }
                else if (per[i] is Korean)
                {
                    ((Korean)per[i]).SayHello();
                }
                else
                {
                    ((Americans)per[i]).SayHello();
                }
                //使用虚方法的情况
                per[i].SayHello();
            }
            Console.ReadKey();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Person   p  = new Person("盘古");
            Chinese  ch = new Chinese("姚明");
            American am = new American("科比");

            Person[] parr = { p, ch, am };
            foreach (var item in parr)
            {
                item.SayHello();
            }
            Console.ReadKey();
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            Chinese  ch1 = new Chinese("Li Lei");
            Chinese  ch2 = new Chinese("Han Meimei");
            American a1  = new American("Rain");
            American a2  = new American("Emma");

            Person[] persons = new Person[] { ch1, ch2, a1, a2 };

            foreach (var item in persons)
            {
                item.Hello();
            }

            Console.ReadKey();
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            Chinese  cn1 = new Chinese("张三");
            Chinese  cn2 = new Chinese("李四");
            Japanese ja  = new Japanese("半泽值树");
            Korea    ko  = new Korea("思密达");
            American am  = new American("川普");
            Person   p   = new Person();//用虚方法实现多态主要原因就是我们需要实例化父类

            Person[] person = new Person[6] {
                cn1, cn2, ja, ko, am, p
            };
            foreach (Person item in person)
            {
                item.SayHello();
            }
            Console.ReadKey();
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            //多态概念:让一个对象表现出多种状态(类型)

            //实现多态主要有三种手段:1、虚方法 2、抽象类 3、接口

            //虚方法:首先将父类的方法标记为虚方法,使用关键字  virtual 。效果就是这个函数可以被子类重新写一遍(可以提取出父类的前提下)


            //当父类方法不知道如何去实现的时候,可以考虑将父类写成抽象类,将方法写成抽象方法
            //抽象类:抽象类的关键字  abstract  方法也一样.  抽象方法是不允许有方法体

            Chinese  chin  = new Chinese("我媳妇");
            Chinese  chin1 = 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 = new Person[] { chin, chin1, j1, j2, k1, k2, a1, a2 };
            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();
            }
        }