2021-02-08

C#多态

实现多态的三种方法
步骤:
1、将父类的方法标记为虚方法 ,使用关键字 virtual,这个函数可以被子类重新写一个遍。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _09多态
{
    class Program
    {
        static void Main(string[] args)
        {
            //概念:让一个对象能够表现出多种的状态(类型)
            //实现多态的3种手段:1、虚方法 2、抽象类 3、接口

            Chinese cn1 = new Chinese("韩梅梅");
            Chinese cn2 = 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 = { cn1, cn2, j1, j2, k1, k2, a1, a2, new English("格林"), new English("玛利亚") };

            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();
            }
            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()
        {
            Console.WriteLine("我是人类");
        }

    }

    public class Chinese : Person
    {
        public Chinese(string name)
            : base(name)
        {

        }

        public override void SayHello()
        {
            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);
        }
    }


    public class English : Person
    {
        public English(string name)
            : base(name)
        { }

        public override void SayHello()
        {
            Console.WriteLine("我是英国人");
        }
    }

}

我们经过virtual关键字标记之后可以实现多态
在这里插入图片描述
使用For循环来循环这个对象数组
完成多态

第二种:
抽象类
当父类中的方法不知道如何去实现的时候,可以考虑将父类写成抽象类,将方法写成抽象方法。

抽象类和抽象方法使用abstract关键字来修饰比如:
在这里插入图片描述
抽象类是不可以创建对象的
在这里插入图片描述
抽象方法是没有方法体的
在这里插入图片描述
我们可以使用抽象类和抽象方法实现多态

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _8抽象类
{
    class Program
    {
        static void Main(string[] args)
        {

            //狗狗会叫 猫咪也会叫

            //Animal a = new Cat(); // new Dog
            Animal a = new Animal();
            //a.Bark();
            Console.ReadKey();
        }
    }
    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("猫咪喵喵的叫");
        }
    }
}

根据里氏转换 我们可以直接把子类赋值给父类 这样可以完成调用:
在这里插入图片描述

到这里差不多就结束了 谢谢大家的观看
本人qq737479476
很喜欢交朋友
以后每天都会更新文章 记录每天学习的笔记
让我们一起加油把

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值