是否有可能在Java中有来自两个不同的类依赖于某些变量的类继承? [英] Is it possible in Java to have a class inherit from two different classes depending on some variable?

查看:135
本文介绍了是否有可能在Java中有来自两个不同的类依赖于某些变量的类继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的父类,从活动的继承和我的所有其他活动,从父类继承。这位家长确实在和的onPause一些onResume生命周期的东西,我所有的活动需要。问题是我有一个地图的活动,必须从Android的MapActivity继承但我仍然需要这个活动让我的父类的生命周期方法。有没有办法有两个父母MapActivity继承?或者,也许部分类我真的不知道在这里。任何想法将是巨大的。

I have a parent class in Android that inherits from Activity and all my other activities inherit from that parent class. This parent does some life cycle stuff in onPause and onResume that all my activities need. The problem is I have a Map activity that must inherit from Android's MapActivity yet I still need this activity to have my parent classes life cycle methods. Is there a way to have the MapActivity inherit from two parents? Or maybe a partial class I'm not really sure here. Any Ideas would be great.

谢谢,

布莱恩

推荐答案

简短的答案是否定的。你不能从Java中的两个类继承的类。该标准的建议是使用接口,但我不认为这是正确的,你在这种情况下。

The short answer is no. You cannot have a class that inherits from two classes in Java. The standard recommendation would be to use an interface, but I don't think that's right for you in this case.

或许,你可以实现你用成分,例如,而不是继承寻找code重用。如果您发布一个code例如,我可以给你一个更具体的答案。

Perhaps that you can achieve the code reuse you are looking for by using composition, for example, instead of inheritance. If you post a code example, I could give you a more specific answer.

这篇关于是否有可能在Java中有来自两个不同的类依赖于某些变量的类继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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