多态学习

一、虚方法

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

复制代码
    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("鸟在飞");
        }
    }
复制代码

 

posted @   昵称已存在嘿  阅读(51)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
欢迎阅读『多态学习』
欢迎阅读『多态学习』
点击右上角即可分享
微信分享提示