What can polymorphism do that inheritance can#39;t?(多态性能做哪些继承不能做的事情?)
问题描述
假设我们有一个类 Animal
,它扩展到其他三个类:Dog
、Cat
、Bird
.
Imagine that we have a class Animal
that extends to three other classes: Dog
, Cat
, Bird
.
这个动物类有一个 talk()
和 move()
函数.talk 函数输出Animal talk",move 函数输出Animal Moving".
This animal class has a talk()
and move()
function. The talk function outputs "Animal talking" and the move function outputs "Animal moving".
对于狗来说,这是狗移动"和狗吃".对于 Cat
和 Bird
类,这种差异是平行的猫移动"等.
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()
然后调用
charlietheBird.talk()
它会输出
鸟语花香
因为输出是在运行时确定的,因为编译器知道 charlie 是 Bird
类的 Animal
类型.
because the output is determined at runtime since the compiler knows that charlie is a type of Animal
of the class Bird
.
但是!!
我可以做到
Bird charlietheBird = new Bird();
然后调用 charlietheBird.poop();
将给出相同的输出,因为方法会被覆盖.
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?
多态性的真正优势可以在运行时而不是编译时看到.多态性允许您将一种实现替换为另一种实现,而无需更改使用它的代码.让我们以 Animal
层次结构为例.假设您有一个知道如何对任何动物进行健康检查的Vet
(是的,他是个超级兽医).
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();
}
}
你现在可以说:
Vet vetWithBird = new Vet(new Bird());
Vet vetWithDog = new Vet(new Dog());
vetWithBird.performCheckup();
vetWithDog.performCheckup();
请注意如何告诉 Vet
在不需要的情况下对 Bird
或 Dog
或任何其他动物进行检查更改您的 Vet
课程.在运行时,Dog
在进行检查时会吠叫,Bird
在进行检查时会发推文.想象一下,如果 Vet
有一个 Bird
引用而不是 Animal
:
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 it goes for a checkup and the Bird
would tweet when it goes for a checkup. 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();
}
}
可怜的 Vet
现在只能与 Bird
一起工作.告诉您的 Vet
与 Dog
一起工作,他会立即拒绝.
The 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 reject this right away.
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
这篇关于多态性能做哪些继承不能做的事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:多态性能做哪些继承不能做的事情?
基础教程推荐
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01