多重继承和多态有什么区别?
多重继承和多态有什么区别?
在一本书中,我红了一行,说
在类级别不支持多重继承。这意味着您一次不能扩展多个类。
这与多态性的概念相矛盾,多态性在同一本书中描述为
多态性是从特定基类创建多个对象的过程 ?
如果 PHP 不允许多重继承,那么如何允许多态呢
What is the difference between Multiple Inheritance and Polymorphism?
In a Book I red a line saying
there is no support for multiple inheritances at class level. This means you can't extend more than one class at a time.
Which is contradicting the concept of Polymorphism, described in the same book as
polymorphism is the process of creating several objects from specific base classes
Now if Multiple Inheritance is not allowed in PHP then how the Polymorphism is allowed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Ikke 所说,多重继承与多态性无关。
如果我可以画一个类图,多重继承看起来像这样:
因此,类
Child
将从两个类继承属性和行为。许多语言(例如 Java 和 PHP)不允许这样做,但 Python 允许。另一方面,多态性是指您可以抽象出专业化。首先,类图:
您可以执行以下操作:
每个
pet
根据实现的不同会说不同的事情。Like Ikke said, Multiple Inheritance has nothing to do with Polymorphism.
If I could draw a class diagram, Multiple Inheritance looks like this:
So, the class
Child
would inherit both attributes and behaviours from both classes. Many languages like Java and PHP don't allow this, but Python does.Polymorphism, on the other hand, is when you can abstract out a specialisation. First of all, class diagram:
And you may do the following:
Each
pet
will say different things depending on the implementation.多重继承意味着一个对象继承自两个不同的父类。程序员自行车手既是程序员又是自行车手。当 Programmer 类将其成员数据
favorite_activity
定义为hacking
而 Bicyclist 也有favorite_activity
,但它是riding
时,就会出现问题代码>.如果您问一位骑自行车的程序员她的favorite_activity
是什么,正确答案是什么?多态性处理对象的行为。它可以让您告诉对象执行某些操作,并使结果操作取决于对象的类,即使您不确切知道那是什么。因此,您遇到了一个人,尽管您不知道他是程序员还是厨师,但您告诉她
perform_your_job()
。如果是程序员,她会编写代码,如果是厨师,她会做饭,但您不必专门告诉她write_code()
或make_a_meal()
。Multiple inheritance means an object inherits from two different parent classes. A ProgrammerBicyclist is both a Programmer and a Bicyclist. The problem arises when the Programmer class defines its member data
favorite_activity
ashacking
while a Bicyclist also hasfavorite_activity
, but it'sriding
. If you ask a ProgrammerBicyclist what herfavorite_activity
is, what's the correct answer?Polymorphism deals with the behavior of objects. It lets you tell an object to do something and have the resulting action depend on the object's class, even if you don't know exactly what that is. So you come across a Person, though you don't know if it's a Programmer or a Cook, and you tell her to
perform_your_job()
. If it's a Programmer she will write code, if it's a Cook she will make a meal, but you don't have to specifically tell her towrite_code()
ormake_a_meal()
.这两个人彼此之间没有什么关系。
多重继承是编译时/运行时之后静态的东西。多态性是一种仅在运行时实际决定调用子类型上的哪个方法的技术。
PHP 不允许多重继承。
Those two have very little to do with each other.
Multiple inheritance is something that is static after compile time / runtime. Polymorphism is a technique where only on runtime actually is decided which method on a subtype is called.
PHP doesn't allow for multiple inheritance.