Java多重继承 [英] Java Multiple Inheritance

查看:146
本文介绍了Java多重继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了完全理解如何解决Java的多重继承问题,我有一个经典的问题需要澄清。

In an attempt to fully understand how to solve Java's multiple inheritance problems I have a classic question that I need clarified.

让我说我有类动物这有子类我需要上课 Pegasus Bird 延伸,因为 Pegasus 既是鸟又是马。

Lets say I have class Animal this has sub classes Bird and Horse and I need to make a class Pegasus that extends from Bird and Horse since Pegasus is both a bird and a horse.

我认为这是经典的钻石问题。根据我的理解,解决这个问题的经典方法是制作 Animal Bird 类接口并实现 Pegasus

I think this is the classic diamond problem. From what I can understand the classic way to solve this is to make the Animal, Bird and Horse classes interfaces and implement Pegasus from them.

我想知道是否有另一种方法可以解决我仍然可以为鸟类和马创建物体的问题。如果有一种方法可以创造动物也很棒但不是必需的。

I was wondering if there was another way to solve the problem in which I can still create objects for birds and horses. If there was a way to be able to create animals also that would be great but not necessary.

推荐答案

你可以创建接口动物类(生物学意义上的类),例如公共接口Equidae 用于马匹和公共接口Avialae 用于鸟类(我不是生物学家,所以条款可能是错误的。)

You could create interfaces for animal classes (class in the biological meaning), such as public interface Equidae for horses and public interface Avialae for birds (I'm no biologist, so the terms may be wrong).

然后你仍然可以创建一个

Then you can still create a

public class Bird implements Avialae {
}

public class Horse implements Equidae {}

以及

public class Pegasus implements Avialae, Equidae {}






从评论中添加:



为了减少重复代码,你可以创建一个抽象类,其中包含你想要实现的动物的大部分常用代码。


Adding from the comments:

In order to reduce duplicate code, you could create an abstract class that contains most of the common code of the animals you want to implement.

public abstract class AbstractHorse implements Equidae {}

public class Horse extends AbstractHorse {}

public class Pegasus extends AbstractHorse implements Avialae {}






更新



我'我想再添加一个细节。正如 Brian评论所述,这是OP已经知道的事情。


Update

I'd like to add one more detail. As Brian remarks, this is something the OP already knew.

但是,我想强调,我建议绕过接口的多继承问题,我不建议使用代表已经存在的接口具体类型(如鸟)但更多的行为(其他人指的是鸭子打字,这也很好,但我的意思是:鸟类的生物类,Avialae)。我也不建议使用以大写字母'I'开头的界面名称,例如 IBird ,它只是告诉你为什么需要一个界面。这就是问题的不同之处:使用接口构造继承层次结构,在有用时使用抽象类,在需要时实现具体类,并在适当时使用委托。

However, I want to emphasize, that I suggest to bypass the "multi-inheritance" problem with interfaces and that I don't recommend to use interfaces that represent already a concrete type (such as Bird) but more a behavior (others refer to duck-typing, which is good, too, but I mean just: the biological class of birds, Avialae). I also don't recommend to use interface names starting with a capital 'I', such as IBird, which just tells nothing about why you need an interface. That's the difference to the question: construct the inheritance hierarchy using interfaces, use abstract classes when useful, implement concrete classes where needed and use delegation if appropriate.

这篇关于Java多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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