• 多态之虚方法


    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);
            }
        }
    }
  • 相关阅读:
    《互联网时代的软件革命-saas架构设计》读书笔记
    iphone11白苹果 解决办法最简单
    idea配置(重新配置)
    力扣刷题03--无重复字符的最长字串
    力扣刷题02--两数相加
    《设计原本》读书笔记(三)
    《设计原本》读书笔记(二)
    《设计原本》读书笔记(一)
    MVC架构科技小论文
    面向服务架构及其应用
  • 原文地址:https://www.cnblogs.com/blacop/p/5962878.html
Copyright © 2020-2023  润新知