• 多态学习


    一、虚方法

    通过一个例子,人类,中国人,日本人自我介绍

        class Program
        {
            static void Main(string[] args)
            {
                Chinese chinese = new Chinese("小明");
                Chinese chinesetwo = new Chinese("小东");
                Japanese japanese = new Japanese("苍井空");
    
    
                Person[] person = { chinese , chinesetwo, japanese };
    
                for (int i = 0; i < person.Length; i++)
                {
                    person[i].SayHello();
                }
                Console.ReadLine();
            }
        }
        /// <summary>
        /// 人类基类
        /// </summary>
        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()
            {
                Console.WriteLine("我是人类");
            }
        }
        /// <summary>
        /// 中国人继承基类
        /// </summary>
        public class Chinese : Person
        {
            public Chinese(string name) : base(name)
            {
    
            }
            public override void SayHello()
            {
                Console.WriteLine("我是中国人,我叫{0}",this.Name);
            }
        }
        /// <summary>
        /// 日本人继承基类
        /// </summary>
        public class Japanese : Person
        {
            public Japanese(string name) : base(name)
            {
    
            }
            public override void SayHello()
            {
                Console.WriteLine("我是日本人,我叫{0}", this.Name);
            }
        }

    二、抽象类

    通过一个例子,狗叫猫叫

    class Program
        {
            static void Main(string[] args)
            {
                Animal animal = new Dog();
                animal.Bark();
                //狗叫、猫叫
                Console.ReadLine();
            }
        }
        /// <summary>
        /// 动物类
        /// </summary>
        public abstract class Animal
        {
            public abstract void Bark();
        }
        public class Dog : Animal
        {
            public override void Bark()
            {
                Console.WriteLine("狗叫");
            }
        }
        public class Cat : Animal
        {
            public override void Bark()
            {
                Console.WriteLine("猫叫");
            }
        }

     三、接口

      class Program
        {
            static void Main(string[] args)
            {
                //IFlyable flyable = new Person();
                IFlyable flyable = new Bird();
                flyable.Fly();
                Console.ReadLine();
            }
        }
        public interface IFlyable
        {
            void Fly();
        }
        public class Person : IFlyable
        {
            public void Fly()
            {
                Console.WriteLine("人在飞");
            }
        }
        public class Bird : IFlyable
        {
            public void Fly()
            {
                Console.WriteLine("鸟在飞");
            }
        }
  • 相关阅读:
    HDU 2121 Ice_cream’s world II 不定根最小树形图
    POJ 3164 Command Network 最小树形图
    POJ 3723 Conscription 最小生成树
    UVA 1175 Ladies' Choice 稳定婚姻问题
    BZOJ 2753 [SCOI2012] 滑雪和时间胶囊 最小生成树
    BZOJ 1854: [Scoi2010]游戏 无向图判环
    HDU 3974 Assign the task 暴力/线段树
    Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路
    uoj 67 新年的毒瘤 割点
    蓝桥
  • 原文地址:https://www.cnblogs.com/liguix/p/14629857.html
Copyright © 2020-2023  润新知