不是多态性只是继承的副作用吗? [英] Isn't polymorphism just a side effect of inheritance?

查看:117
本文介绍了不是多态性只是继承的副作用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我理解正确,继承定义一个类型层次结构和类之间的关系。子类继承父类的行为,可以提供父类上任何公共接口的专门行为。



多态是这些子类可以提供自己的行为,同时仍然坚持



$ b

这是同样的事情吗?

解决方案

您是正确的,在大多数OO语言中,继承和多态性与另一种语言一致。但是:


  1. 多态性和继承都说明了不同的方面。多态性显示了类型可以如何操作,而继承显示类型之间的关系。

  2. 多态和继承不一定一起 如果你看整数和浮点数,例如,它们都可以在算术表达式的相同的地方,所以它实际上是多态性,但没有继承。另外,继承可能没有多态性。


Im learning about oop just now and am struggling with the difference between inheritance and polymorphism.

If i understand correctly, inheritance defines a type hierarchy and the relationship between classes. Child classes inherit behaviour from the parent class and can provide specialised behaviour of any public interface on the parent.

Polymorphism is that these child classes can provide their own behaviour while still adhering to the parent interface.

This is the same thing isnt it?

解决方案

You are correct that in most OO languages, inheritance and polymorphism go one with another. But:

  1. Polymorphism and Inheritance both illustrate different aspects. Polymorhpism shows how the types can stand one for another in operations, while the inheritance shows the relationship between the types.
  2. Polymorphism and Inheritance don't have to go together in general. If you look at integer and float numbers for example, there all of them can stand at the same places of arithmetic expression, so it is in fact polymorphism, but there is no inheritance. Also, inheritance is possible without polymorphism.

这篇关于不是多态性只是继承的副作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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