Java中多重继承的替代方案
我已经创建了两个 bean
class BackPageBean{
String backPage = null;
:
:
:
}
class InformationMessageBean{
String informationMessage = null;
:
:
:
}
现在,如果一个类是 backpage 感知的,那么它将扩展 backPageBean,或者如果它需要显示某种消息,那么它将扩展 InformationMessageBean。
class BackPageAware extends backPageBean{
}
class InfoMessAware extends InformationMessageBean{
}
someFunction () {
if ( theObject instanceOf backPageBean ) {
prepareTheBackPage ( theObject.getBackPage() );
}
if ( theObject instanceOf InformationMessageBean ) {
showtheInformation ( theObject.getMessage() );
}
}
现在的问题是,如果我想要一个既是 BackPageAware 又是 InformationAware 的 bean,那么,由于我们没有多重继承,应该采取什么方法呢?
I have created two beans
class BackPageBean{
String backPage = null;
:
:
:
}
class InformationMessageBean{
String informationMessage = null;
:
:
:
}
Now, if a class is backpage aware then it will extend backPageBean, or if it need to show some kind of message then it extends InformationMessageBean.
class BackPageAware extends backPageBean{
}
class InfoMessAware extends InformationMessageBean{
}
someFunction () {
if ( theObject instanceOf backPageBean ) {
prepareTheBackPage ( theObject.getBackPage() );
}
if ( theObject instanceOf InformationMessageBean ) {
showtheInformation ( theObject.getMessage() );
}
}
Now the problem is, if i want to have a bean which is both BackPageAware as well as InformationAware then, as we don't have multiple inheritance, what should be the approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
只是为了澄清我的评论。
Just to clarify my comment.
使用接口:
然后用标准方法调用替换 instanceof 。
use interfaces:
then replace instanceof with standard method calls.
您所描述的问题需要使用组合,而不是继承。 BackPageAware 类意味着它知道该类/功能。 继承意味着它是一个BackPage。 您描述了一种HAS关系。
正如已经多次说过的那样,使用接口来定义用于检索对象拥有信息的契约。
The problem you are describing begs the usage of composition, not inheritance. The class being BackPageAware means it knows about that class/functionality. Inheritance means it IS a BackPage. You have described a HAS A relationship.
As has been said many times now, use interfaces to define the contracts for retrieving the information that the object HAS.
您的两个原始类应该是接口,每个类都有一个方法,用于检索实现应返回的信息。
如果您希望一个类同时实现 BackPageBean 和 InformationMessageBean,您只需这样做:
一般来说,您应该尽可能避免扩展非抽象类,这会导致各种问题。 相反,在涉及具体类的情况下,尝试使用组合而不是继承,否则,尝试坚持使用接口和偶尔的抽象类。
Your two original classes should be Interfaces, each with a method on them which retrieves the information that implementations should return.
If you want a class to implement both BackPageBean and InformationMessageBean you simply do this:
Generally speaking, you should avoid extending non-abstract classes whenever you can, it leads to all sorts of problems. Instead, try using composition instead of inheritance where concrete classes are involved, and otherwise, try and stick to interfaces and the occasional Abstract class.
正如其他答案中所讨论的,可以使用接口和组合来模拟多重继承,但代价是必须编写大量样板代码。 然而,有许多项目可以在编译时(通过预处理器)或运行时自动执行此操作,例如 jmixin。
As discussed in other answers multiple inheritance can be simulated using interfaces and composition, at the expense of having to write a lot of boilerplate code. There are however a number of projects around that can automate this either at compile time (via a pre-processor) or at runtime eg jmixin.
您可以将
BackPageAware
和InformationAware
作为接口而不是类。 如果您引用的“bean”是BackPageAware
和InformationAware
,这使您能够从两个接口获取消息。You can have
BackPageAware
andInformationAware
as interfaces rather than classes. This enables you to then fetch the messages from both interfaces in the case that the 'bean' you are referring to is ofBackPageAware
andInformationAware
.将 BackPageAware 和 InformationAware 声明为接口,创建实现这些接口的抽象类以提供默认功能。 任何只需两个接口之一的类都可以继承相应的抽象类。 需要两者的类可以继承其中一个抽象类并实现另一个接口,或者实现两个接口,等等。 如果没有默认功能,那么您甚至不需要抽象类。
Declare BackPageAware and InformationAware as interfaces, create abstract classes that implement those interfaces to provide the default functionality. Any class that needs to be only one of the two interfaces can inherit the corresponding abstract class. A class that needs to be both can inherit one of the abstract classes and implement the other interface, or implement both interfaces, whatever. If there is no default functionality, then you don't even need the abstract classes.
您不能扩展多个类,但可以实现多个接口。 请记住,当您将接口应用于类时,该类的对象与该接口具有 IS-A 关系。
You can't extend multiple classes but you can implement multiple interfaces. Remember that when you apply an interface to a class, the object of the class has a IS-A relationship with the interface.