15-02-多态的应用

2021-04-08  本文已影响0人  D丝学编程

多态: 多态是同一个行为具有多个不同表现形式或形态的能力。

多态的应用场景:

(1)将父类作为参数进行传递。

(2)将父类作为返回值。

案例一:将父类作为参数传递(模拟人和动物打招呼)

首先看如下代码的缺陷:

class Dog
{
    public void Speaking()
    {
        Console.WriteLine("汪汪汪。。。!");
    }
}
class Cat
{
    public void Speaking()
    {
        Console.WriteLine("喵喵喵喵.......");
    }
}
class People
{
    //人类给狗打招呼
    public void SayHi(Dog dog)
    {
        dog.Speaking();
    }

    //人类给猫打招呼
    public void SayHi(Cat cat)
    {
        cat.Speaking();
    }
}

此时有一个People(人)类,需要实现,和动物打招呼的方法,方法需要传递一个动物进来,那么此时系统中有n种东西,在People类中就需要定义n个方法进行重载。

以下是main方法的调用测试:

Cat cat = new Cat();
Dog dog = new Dog();
People p = new People();
p.SayHi(cat);
p.SayHi(dog);

解决方案:将父类作为参数传递实现多态,如下代码:

class Animal
{
    public virtual void Speaking()
    {
        Console.WriteLine("Hello!");
    }
}
class Dog:Animal
{
    public override void Speaking()
    {
        Console.WriteLine("汪汪汪。。。!");
    }
}
class Cat:Animal
{
    public override void Speaking()
    {
        Console.WriteLine("喵喵喵喵.......");
    }
}
class People
{
    //人类给所有动物打招呼
    public void SayHi(Animal animal)
    {
        animal.Speaking();
    }
}

此时,People类中的SayHi方法接收父类作为参数,不管系统中有多少种动物,此时SayHi只用编写一次,实现的多态的思想。

以下是main方法的调用测试:

Animal cat = new Cat();
Animal dog = new Dog();
People p = new People();
p.SayHi(cat);
p.SayHi(dog);

案例二:子类 is—a 父类 (模拟人和动物玩游戏,和狗玩飞盘,和猫玩捉迷藏)

class Animal 
{
    public string name;
}
class Dog : Animal
{
    //接飞盘游戏
    public void CatchFlyDisc()
    {
        Console.WriteLine("我和主人正在玩接飞盘的游戏...");
    }
}
class Cat : Animal
{
    //玩捉迷藏游戏
    public void HideAndSeek()
    {
        Console.WriteLine("我和主人正在玩捉迷藏的游戏");
    }
}
class People 
{
    //和动物玩游戏
    public void Play(Animal animal)
    {
        if(animal is Dog)
        {
            Dog dog = (Dog)animal;
            dog.CatchFlyDisc();
        }
        if(animal is Cat)
        {
            Cat cat = (Cat)animal;
            cat.HideAndSeek();
        }       
    }   
}

此处传递一个动物到Play方法中,但是由于接飞盘和捉迷藏的方法并没有从父类重写,方法名不同,所以在具体实现的时候必须要知道传递过来的animal到底是狗还是猫,可以使用is来进行判断。

在main方法中的调用:

Animal animal = new Cat();
People p = new People();
p.Play(animal);   //此时会调用Cat类中的HideAndSeek方法打印消息。

案例三:将父类作为返回值(模拟人领养动物)

首先看如下代码缺陷:

class Dog
{
    public string name;
}
class Cat
{
    public string name;
}
public class People 
{
    //此处领养狗的方法
    public Dog OwnDog()
    {
        Dog dog = new Dog();
        dog.name = "snoopy";
        return dog;
    }   
    //此处领养猫的方法
    public Cat OwnCat()
    {
        Cat cat = new Cat();
        cat.name = "tom";
        return cat;
    }
}

此处缺点在于,People中领养动物的方法,系统中有n中动物,就必须编写n个方法。

在main方法中的调用:

Console.WriteLine("请输入您要领养的动物类型:(1-狗,2-猫)");
int type = int.Parse(Console.ReadLine());
People p = new People();
if(type == 1)
{
    p.OwnDog();
    Console.WriteLine("恭喜您领养了一只狗!");
}
if(type == 2)
{
    p.OwnCat();
    Console.WriteLine("恭喜您领养了一只猫!");
}

解决方案:将父类作为返回值实现多态,如下代码:

class Animal 
{
    public string name;
}
class Dog : Animal
{
}
class Cat : Animal
{
}
class People 
{
    //父类作为返回值
    public Animal OwnAnimal(int type)
    {
        Animal animal;
        if(type == 1)
        {
            animal = new Dog();
            animal.name="snoopy";
        }
        else if(type == 2)
        {
            animal = new Cat();
            animal.name="tom";
        }
        else
        {
            animal = null;
        }
        return animal;
    }
}

此时无论系统中有多少种动物,一个方法就可以搞定。

在main方法中的调用:

Console.WriteLine("请输入您要领养的动物类型:(1-狗,2-猫)");
int type = int.Parse(Console.ReadLine());
People p = new People();
Animal animal = p.OwnAnimal(type);
Console.WriteLine("恭喜您领养动物成功!");
上一篇 下一篇

猜你喜欢

热点阅读