从两个类扩展 [英] Extending from two classes

查看:30
本文介绍了从两个类扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该怎么做:

public class Main extends ListActivity , ControlMenu 

另外,我想知道这种方法是否可行,因为我已经在 ControlMenu 类中制作了菜单,并且我正在扩展其余的活动.

Also, I would like to know that is this approach is okay that I have made the menus in class which is ControlMenu and I am extending in rest of the activities.

推荐答案

您只能扩展一个类.并从许多来源实现接口.

You can only Extend a single class. And implement Interfaces from many sources.

无法扩展多个类.我能想到的唯一解决方案是不继承任何一个类,而是拥有每个类的内部变量,并通过将请求重定向到您的对象到您希望它们去的对象来做更多的代理.

Extending multiple classes is not available. The only solution I can think of is not inheriting either class but instead having an internal variable of each class and doing more of a proxy by redirecting the requests to your object to the object that you want them to go to.

 public class CustomActivity extends Activity {

     private AnotherClass mClass;

     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         mClass = new AnotherClass(this);
     }

     //Implement each method you want to use.
     public String getInfoFromOtherClass()
     {
        return mClass.getInfoFromOtherClass();
     }
 }

这是我想出的最佳解决方案.您可以从两个类中获取功能,但实际上仍然只能属于一种类类型.

this is the best solution I have come up with. You can get the functionality from both classes and Still only actually be of one class type.

缺点是您无法使用演员表适应内部类的模具.

The drawback is that you cannot fit into the Mold of the Internal class using a cast.

这篇关于从两个类扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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