多重继承和多态有什么区别?

发布于 2024-08-30 03:46:29 字数 207 浏览 2 评论 0原文

多重继承和多态有什么区别?

在一本书中,我红了一行,说

在类级别不支持多重继承。这意味着您一次不能扩展多个类。

这与多态性的概念相矛盾,多态性在同一本书中描述为

多态性是从特定基类创建多个对象的过程

如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无所的.畏惧 2024-09-06 03:46:29

正如 Ikke 所说,多重继承与多态性无关。

如果我可以画一个类图,多重继承看起来像这样:

Base A    Base B
  ^         ^
   \       /
    \     /
     Child

因此,类 Child 将从两个类继承属性和行为。许多语言(例如 Java 和 PHP)不允许这样做,但 Python 允许。

另一方面,多态性是指您可以抽象出专业化。首先,类图:

     Animal
     ^    ^
    /      \
   /        \
 Cat        Dog

您可以执行以下操作:

// Assuming we have a pack of animals
// This is Java
for (Animal pet : pack)
    pet.speak();

每个 pet 根据实现的不同会说不同的事情。

Like Ikke said, Multiple Inheritance has nothing to do with Polymorphism.

If I could draw a class diagram, Multiple Inheritance looks like this:

Base A    Base B
  ^         ^
   \       /
    \     /
     Child

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:

     Animal
     ^    ^
    /      \
   /        \
 Cat        Dog

And you may do the following:

// Assuming we have a pack of animals
// This is Java
for (Animal pet : pack)
    pet.speak();

Each pet will say different things depending on the implementation.

饭团 2024-09-06 03:46:29

多重继承意味着一个对象继承自两个不同的父类。程序员自行车手既是程序员又是自行车手。当 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 as hacking while a Bicyclist also has favorite_activity, but it's riding. If you ask a ProgrammerBicyclist what her favorite_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 to write_code() or make_a_meal().

无声情话 2024-09-06 03:46:29

这两个人彼此之间没有什么关系。

多重继承是编译时/运行时之后静态的东西。多态性是一种仅在运行时实际决定调用子类型上的哪个方法的技术。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文