多态可以做什么继承不行? [英] What can polymorphism do that inheritance can't?

查看:103
本文介绍了多态可以做什么继承不行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我们有一个类 Animal ,扩展到另外三个类: Dog Bird



这个动物类有一个 talk() move()函数。通话功能输出动物交谈,移动功能输出动物移动。



对于狗,这是狗移动和狗吃。对于 Cat Bird 类,这种差异与猫移动等同。



现在,由于多态性,如果我做

 动物charlietheBird = new Bird()

然后调用

 code> charlietheBird.talk()

它将输出


鸟谈话


因为输出是在运行时确定的,因为编译器知道那个charlie是一种类型 Animal 的课程 Bird





我可以简单地做

鸟类(B)=新鸟(); 

然后调用 charlietheBird.poop(); 将给出相同的输出,因为
方法将被覆盖。

解决方案


多态可以做什么继承不能?


多态性的真正优势可以在运行时而不是编译时间。多态可以让一个实现替代另一个,而无需更改使用它的代码。我们来看看你的例子 Animal 层次结构。假设你有一个 Vet ,知道如何对任何动物进行健康检查(Yup他是一个超级)。

  class Vet {
private Animal animal;
public Vet(动物){
this.animal = animal;
}

public void perfromCheckup(){
animal.talk();
animal.poop();
}
}

您现在可以说:

  Vet vetWithBird = new Vet(new Bird()); 
Vet vetWithDog = new Vet(new Dog());
vetWithBird.performCheckup();
vetWithDog.performCheckup();注意你可以如何告诉 Vet Bird Dog 或任何其他动物进行检查,无需更改您的 Vet class。在运行时,当他进行检查时, Dog 将会吠叫,当他进行检查时, Bird 会鸣叫。现在想象如果不是 Animal ,那么 Vet 有一个 Bird 参考:

  class Vet {
private Bird bird;
public Vet(鸟鸟){
this.bird = bird;
}

public void perfromCheckup(){
bird.talk();
bird.poop();
}
}

您的穷人 Vet 现在只能使用 Bird 。告诉你的 Vet 使用 Dog ,他会明白拒绝。

  Vet vet = vetWithBird = new Vet(new Bird()); //工作正常。兽医喜欢鸟
Vet vet = new Vet(new Dog())//编译错误。对不起,我不喜欢狗

总之,多态允许您替换子类实例,使用类引用。继承允许您从父类继承代码,并可能在子类中重新定义该行为,以便您的代码可以在运行时通过多态


Imagine that we have a class Animal that extends to three other classes: Dog, Cat, Bird.

This animal class has a talk() and move() function. The talk function outputs "Animal talking" and the move function outputs "Animal moving".

For a dog, this is "Dog moving" and "Dog eating". For the Cat and Bird class, this difference is paralleled "Cat moving" etc.

Now, because of polymorphism, if I do

Animal charlietheBird = new Bird()

and then call in

charlietheBird.talk()

it will output

Bird talking

because the output is determined at runtime since the compiler knows that charlie is a type of Animal of the class Bird.

HOWEVER!!

I can simply do

Bird charlietheBird = new Bird();

and then calling charlietheBird.poop(); will give the same output, because the method would have been overridden.

解决方案

What can polymorphism do that inheritance can't?

The real advantages of Polymorphism can be seen at runtime rather than compile time. Polymorphism allows you to substitute one implementation for another without the need to change the code that uses it. Let's take your example of the Animal hierarchy. Let's say you have a Vet that knows how to perform health checkups on any animal (Yup he's a supervet).

class Vet {
   private Animal animal; 
   public Vet(Animal animal) {
      this.animal = animal;
   }

   public void perfromCheckup() {
      animal.talk();
      animal.poop();
   }
} 

You can now say :

Vet vetWithBird = new Vet(new Bird());
Vet vetWithDog =  new Vet(new Dog());
vetWithBird.performCheckup();
vetWithDog.performCheckup();

Notice how you can tell the Vet to perform a checkup on a Bird or a Dog or any other animal for that matter without needing to change your Vet class. At runtime, the Dog would bark when he goes for a checkup and the Bird would tweet when he goes for a checkup. Now imagine if instead of Animal, the Vet had a Bird reference :

class Vet {
   private Bird bird; 
   public Vet(Bird bird) {
      this.bird = bird;
   }

   public void perfromCheckup() {
      bird.talk();
      bird.poop();
   }
} 

Your poor Vet is now only going to be able to work with a Bird. Tell your Vet to work with a Dog and he will plainly reject it.

Vet vet = vetWithBird = new Vet(new Bird()); //Works fine. Vet likes birds.
Vet vet = new Vet(new Dog())// compilation error. Sorry I don't like dogs.

In summary, Polymorphism allows you to substitute subclass instances where a super-class reference is used. Inheritance allows you to inherit code from a parent class and possibly redefine that behavior in subclasses so that your code can take advantage of it at runtime through Polymorphism

这篇关于多态可以做什么继承不行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆