C#中多态的实现

2021-05-29 03:02

阅读:927

标签:pre   base   ice   mamicode   父类   mic   err   void   常见   

由于可以继承基类的所有成员,子类就都有了相同的行为,但是有时子类的某些行为需要相互区别,而子类需要覆盖父类的方法来实现子类特有的行为,这就是所谓的多态,多态即相同类型的对象调用相同的方法却表现出不同行为的现象。

一.实现多态的两种常见方式

(1).虚方法(virtual):将父类的方法,添加关键字virtual,此方法在子类中用override重写。

(2).抽象类与抽象方法(abstarct):有时候基类的作用只是为子类提供公共成员,没有具体的实现操作,那么此时可以将基类及其方法定义为抽象的。子类中的方法仍用override重写。

 

二.虚方法(virtual)的使用

我们知道比较有名的车,有宝马,奔驰等,可以从它们中抽象出一个汽车基类,而宝马车,奔驰车作为子类

(1).基类

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

namespace Polymorphic
{
    public class Car
    {
        public virtual void carName()
        {
            Console.WriteLine("这是汽车");
        }
    }
}

(2).子类

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

namespace Polymorphic
{
    public class BaoMa:Car
    {
        public override void carName()
        {
            //base.carName();
            Console.WriteLine("这是宝马汽车");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Polymorphic
{
    public class BenChi:Car
    {
        public override void carName()
        {
            //base.carName();
            Console.WriteLine("这是奔驰汽车");
        }
    }
}

(3).调用

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

namespace Polymorphic
{
    class Program
    {
        static void Main(string[] args)
        {
            Car car = new Car();
            BenChi benChi = new BenChi();
            BaoMa baoMa = new BaoMa();
            Car[] cars = { car, benChi, baoMa };
            foreach (Car c in cars)
            {
                c.carName();
            }
            Console.ReadKey();
        }
    }
}

打印结果:技术图片

三.关键字abstract实现多态

动物分很多种,比如说牛,马等,但它们发出的声音都是不相同的。

(1).基类

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

namespace Polymorphic
{
    public abstract class Animal
    {
        //动物声音
        public abstract void Voice();
    }
}

(2).子类

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

namespace Polymorphic
{
    class Horse:Animal
    {
        //马的声音
        public override void Voice()
        {
            Console.WriteLine("马的声音");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Polymorphic
{
    class Cattle:Animal
    {
        //牛的声音
        public override void Voice()
        {
            Console.WriteLine("牛的声音");
        }
    }
}

(3).调用

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

namespace Polymorphic
{
    class Program
    {
        static void Main(string[] args)
        {
            Horse horse = new Horse();
            Cattle cattle = new Cattle();
            Animal[] animals = { horse, cattle };
            foreach (Animal animal in animals)
            {
                animal.Voice();
            }
            Console.ReadKey();
        }
    }
}

打印结果:技术图片

 

C#中多态的实现

标签:pre   base   ice   mamicode   父类   mic   err   void   常见   

原文地址:https://www.cnblogs.com/QingYiShouJiuRen/p/11106022.html

上一篇:C#版本号管理

下一篇:NET Core 3.0中的WPF


评论


亲,登录后才可以留言!