本文介绍了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.

假设我有 Animal 类,它有子类 BirdHorse,我需要创建一个类 PegasusBirdHorse 扩展而来,因为 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.

我认为这是经典的钻石问题.据我所知,解决这个问题的经典方法是制作AnimalBirdHorse 类接口并实现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.

推荐答案

你可以为动物类(生物学意义上的类)创建接口,例如public interface 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.

但是,我想强调的是,我建议绕过接口的多继承"问题,并且我不建议使用已经代表了具体类型(例如 Bird)的接口,而是使用更多的行为(其他人指的是鸭子类型,这也很好,但我的意思只是:鸟类的生物学类别,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多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:43