zoukankan      html  css  js  c++  java
  • 多态之虚方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication4
    {
        class Program
        {
            static void Main(string[] args)
            {            
                Chinese c1 = new Chinese("韩梅梅");
                Chinese c2 = new Chinese("李雷");
                Japanese j1 = new Japanese("树下");
                Japanese j2 = new Japanese("田中");
                Korea k1 = new Korea("金秀贤");
                Korea k2 = new Korea("金秀贤2");
                American a1 = new American("科比1");
                American a2 = new American("科比2");
                Person[] pers = {c1,c2,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 if(pers[i] is American)
                    //{
                    //    ((American)pers[i]).SayHello();
                    //}
                    pers[i].SayHello();
                }
                Console.ReadKey();
            }
        }
        public class Person
        {
            private string _name;//字段
    
            public string Name//属性
            {
                get{return _name;}
                set{_name = value;}
            }
    
            public Person(string name)//构造函数
            {
                this.Name = name;
            }
    
            public virtual void SayHello()//方法 virtual 虚方法,这个方法可以被子类重写
            {
                Console.WriteLine("我是人类");
            }
        }
    
        public class Chinese:Person
        {
            public Chinese(string name):base(name) //调用父类构造函数,外面传进一个name,传给父类一个name
            {
                //不用写代码,父类的构造器已经赋值给属性Name
            }
            public override void SayHello()//override重写 父类方法
            {
                Console.WriteLine("我是中国人,我叫{0}",this.Name);
            }        
        }    
        public class Japanese:Person
        {
            public Japanese(string name)
                :base(name)
            { }
            public override void SayHello()
            {
                Console.WriteLine("我是霓虹人,我叫{0}",this.Name);
            }
        }
        public class Korea:Person
        {
            public Korea(string name)
                :base(name)
            { }
            public override void SayHello()
            {
                Console.WriteLine("我是棒子人,我叫{0}",this.Name);
            }
        }
        public class American:Person
        {
            public American(string name)
                :base(name)
            { }
            public override void SayHello()
            {
                Console.WriteLine("我是米国人,我叫{0}",this.Name);
            }
        }
    }
  • 相关阅读:
    小技巧:通过linux一行命令修改ip
    小技巧:textarea文本输入区内实现换行
    set 排序实例
    每日日报
    每日日报
    每日日报
    每日日报
    每日日报
    每日日报
    每日日报
  • 原文地址:https://www.cnblogs.com/blacop/p/5962878.html
Copyright © 2011-2022 走看看