问题描述
我有这样的结构:
1级
Class1
的Class2 Class1的延伸
Class2 extends Class1
我有实用类上采取了Class2中做各种工作,一边一堆。
I have a pile of utility classes on the side that take a Class2 to do all kinds of work.
问题:我需要创建扩展Class2中使用这些班在Class3。在Class3将有一个谷歌API的Android的MapView。 MapViews只能从一个类扩展MapActivity创建。我需要
The problem: I need to create a Class3 extending Class2 to use those classes. Class3 will have a Google API's Android MapView. MapViews can ONLY be created FROM A CLASS EXTENDING MapActivity. I would need
在Class3扩展的Class2和MapActivity。
不能进行。
更改我的3类只延长MapActivity会要求我修改code从previous开发商囤积继承(没有双关语意)。
Changing my class3 to only extend MapActivity would require me to modify a hoard of code inherited (no pun intended) from previous developers.
更改我的CLASS3因为地图需要MapActivity只延长Class2中休息。
Changing my class3 to only extend Class2 breaks because the map requires a MapActivity.
什么是最好的办法?
编辑:一些人所说的接口解决方案。听起来像是正确的解决方案。我忘了提,虽然,Class2中是一个的摘要的类,它会来回移动实现的方法有疼痛感。很多code重复......那张干湿在这一个...:)
Several people mentioned the 'interface solution'. Sounds like the right solution. I forgot to mention, though, that Class2 is an abstract class, and it will be a pain to move the implemented methods around. A lot of code repetition... DRY goes wet on this one... :)
推荐答案
由于Java不支持多重继承,你需要周围的工作。如果我是你,我会作出这样的定义了所有你需要的东西,然后就扩展接口的 1级
Since Java does not support multiple inheritance, you need to make a work around. If I was you I would make an interface that defines all of the things you need and then just extend your Class1
或
您可以简单地让 1级
延长 MapActivity
,除非它已经扩展了别的东西,但是你真的只是想这样做,如果类2
应延伸 MapActivity
。你的类2
可以延长 1级
和您的在Class3
可以延长 1级
和 1级
可以延长 MapActivity
。
You could simply make Class1
extend MapActivity
unless it already extends something else, but you would really only want to do this if Class2
should extend MapActivity
. Your Class2
could extend Class1
and your Class3
could extend Class1
and Class1
could extend MapActivity
.
希望这有助于!
这篇关于Java的多重继承的设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!