简体   繁体   English

如何使用接口在Java中实现多重继承?

[英]How to implement multi inheritance in java with interfaces?

I need to mimic multi inheritance in Java. 我需要模仿Java中的多重继承。 It might be wrong design but, since my problem is not with (parent) class functionality, I have not been able to mimic the feature with interfaces. 这可能是错误的设计,但是由于我的问题不在于(父)类功能,所以我无法使用接口来模仿该功能。 Here is more description on the problem. 这是有关该问题的更多描述。 I have a class called AbstractModel.java with number of methods. 我有一个带有许多方法的名为AbstractModel.java的类。 On the other hand, there is another class AbstractTableModel.java . 另一方面,还有另一个类AbstractTableModel.java Now assume that there is a class called table controller.java that deals with AbstractModel.java and on the other hand there is class called Tableview .java that deals with AbstractTableModel.java . 现在假设有一个名为table controller.java的类,它处理AbstractModel.java ,另一方面,有一个名为Tableview .java的类,它处理AbstractTableModel.java I need have to some way to define : public class A extends AbstractModel , AbstracTableModel so that both view and controller can use the same class with extension. 我需要定义一些方法:公共类A扩展了AbstractModelAbstracTableModel以便视图和控制器都可以使用具有扩展名的同一类。 Please note that the solution AbstractTableModel.java extends AbstractModel is not a solution since it is a built-in java class. 请注意,解决方案AbstractTableModel.java扩展AbstractModel不是解决方案,因为它是内置的Java类。

Thank you. 谢谢。

You cannot do it directly in Java but you can use delegation design pattern . 您不能直接在Java中执行此操作,但可以使用委托设计模式 Look here . 这里 It is a very similar question on SO. 关于SO,这是一个非常相似的问题。

Change your code that expects an AbstractTableModel so that it accepts a TableModel instead (this is an interface implemented by AbstractTableModel ). 更改需要AbstractTableModel代码,使其改为接受TableModel (这是由AbstractTableModel实现的接口)。 This should be done anyway. 无论如何应该这样做。 Never depend on an concrete implementation when you can instead depend on an interface. 当您可以依赖于接口时,请不要依赖于具体的实现。

Then you can easily let class AbstractModel implement TableModel . 然后,您可以轻松地让AbstractModel类实现TableModel

You should probably also think about creating an interface for AbstractModel . 您可能还应该考虑为AbstractModel创建接口。 Usually one would create an interface X , an abstract base implementation AbstractX which provides common functionality shared by all implementations, and implementations MyX , YourX etc. Only the concrete implementations (but not necessarily all of them) would know and depend on AbstractX , but no code outside this hierarchy. 通常,人们会创建一个接口X ,一个抽象的基本实现AbstractX ,它提供所有实现以及实现MyXYourX等共享的通用功能。只有具体的实现(但不一定是全部)才知道并依赖AbstractX ,但是没有此层次结构之外的代码。

The Java Collections Framework is a good example of this: There is for example List , AbstractList , ArrayList and LinkedList , but there is no code that actually depends on an AbstractList . Java Collections Framework是一个很好的例子:例如有ListAbstractListArrayListLinkedList ,但是没有任何代码实际上依赖AbstractList Most users of a list just use any List , or if they need a concrete implementation for some reason, they depend on that. 列表的大多数用户只使用任何List ,或者如果出于某种原因需要具体的实现,则依赖于此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM