如何处理两个类似子类的继承? [英] How to handle inheritance from two similar sub-classes?

查看:218
本文介绍了如何处理两个类似子类的继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个系列中使用了前两个视频了解一些基本的OOP概念。

I've used the first two videos in this series to learn about some basic OOP concepts.

最近,我主要是在Node中写的,所以我在前端和后端使用原型继承。但是,这些教程使用Java展示了OOP概念。 Java是一种严格类型的语言,它使用经典继承

Lately, I primarily write in Node, so I'm working with prototypical inheritance on the front-end and back-end. However, these tutorials showcase OOP concepts with Java. Java is a strictly-typed language which utilizes classical inheritance.

这个问题属于经典和原型继承,但方式不同。

This question pertains to both classical and prototypical inheritance, but in different ways.

这个问题有点难以理解,所以我将使用一个例子:

This problem is a little bit difficult to put into words, so I'll use an example:

我创建了一个名为动物的超类即可。然后我创建了两个动物子类:。现在我的程序需要两个子类的混合。创建 mule 实际上似乎有点棘手。

I've created a super-class called animal. I then create two sub-classes of animal: horse and donkey. Now my program requires a hybrid of the two sub-classes. Creating a mule actually seems to be a little tricky.

起初答案似乎很明显;创建一个独立的骡子类。但那种打败了OOP的目的。当我已经具有这些特征时创建一个新的子类是违反DRY原则

At first the answer seemed obvious; create a stand-alone mule sub-class. But that kind of defeats the purpose of OOP. Creating a new sub-class when I already have the traits is a violation of the DRY principle.

要确认这是创建 mule 的合适方式,我问自己两个问题:

To confirm that this is an appropriate way to create my mule I asked myself two questions:

1)骡子是吗?

2)骡子是吗?

答案似乎是一种响亮的,倾向于

The answer seemed to be a resounding kind of that leans towards a yes.

我完全不知道如何用经典继承来实现这一点。我无法想出我认为具有接口或抽象类的解决方案。

I'm completely lost as to how this would be accomplished with classical inheritance. I could not come up with what I considered a "good" solution with interfaces or abstract classes.

使用的语言典型的继承就像JavaScript一样,我可能只通过拉下应用于 mule 的方法和实例变量来选择性地繁殖骡子。然而,这似乎与创建一个全新的子类非常接近。

In a language which use prototypical inheritance like JavaScript, I might "selectively breed" a mule by pulling down only the methods and instance variables that applied to a mule. However, this seems to be rather close to creating a brand-new sub-class.

经典原型中处理此问题的正确方法是什么? strong>继承?

What is the "correct" way to handle this problem in both classical and prototypical inheritance?

推荐答案

您正在寻找的概念是特征(您实际上提到过它) 。我将使用一个不同的例子,我觉得更合适:

The concept you are looking for is traits (you actually mentioned it). I will use a different example, that I find more appropriate:

trait Engine {
    public function startEngine() {
        echo 'Vrooom';
    }
}

trait Saddle {
    public function rideOnSaddle() {
        echo 'I feel the wind';
    }
}

interface Vehicle {
    public function go();
}

class Car extends Vehicle {
    use Engine;

    public function go() {
        echo $this->startEngine();
    }
}

class Bike extends Vehicle {
    use Saddle;

    public function go() {
        echo $this->rideOnSaddle();
    }
}

class Motorcycle extends Vehicle {
    use Engine;
    use Saddle;

    public function go() {
        echo $this->startEngine();
        echo $this->rideOnSaddle(); 
    }
}

进一步阅读: PHP中的特征 Javascript中的特征

这篇关于如何处理两个类似子类的继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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