可一个类扩展两个类? [英] Can one class extend two classes?

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

问题描述

我的类应该扩展两个类在同一时间:

 公共类preferences延伸AbstractBillingActivity {

公共类preferences扩展preferenceActivity {
 

怎么办呢?

UPD 。由于这是不可能的,我应该怎么使用 AbstractBillingActivity 用preferences呢?

UPD2 。如果我去的接口,应创建:

  1. BillingInterface

     公共接口BillingInterface扩展preferenceActivity,AbstractBillingActivity {
    
    }
     

  2. preferenceActivity

     公共接口preferenceActivity {
    
    }
     

  3. AbstractBillingActivity

     公共接口AbstractBillingActivity {
    
            无效的onCreate(包savedInstanceState);
    
    }
     

然后

 公共类preferences实现BillingInterface {
 

解决方案

Java不支持多重继承。

有几个解决方法我能想到的:

首先是聚集:做一个类,需要这两个活动领域

二是使用接口。

三是要重新考虑你的设计:是否有意义了 preferences 类既是 preferenceActivity 的的 AbstractBillingActivity

My class should extend two classes at the same time:

public class Preferences extends AbstractBillingActivity {

public class Preferences extends PreferenceActivity {

How to do so?

Upd. Since this is not possible, how should I use that AbstractBillingActivity with Preferences then?

Upd2. If I go with interfaces, should I create:

  1. BillingInterface

    public interface BillingInterface extends PreferenceActivity, AbstractBillingActivity {
    
    }
    

  2. PreferenceActivity

    public interface PreferenceActivity {
    
    }
    

  3. AbstractBillingActivity

    public interface AbstractBillingActivity {
    
            void onCreate(Bundle savedInstanceState);
    
    }
    

and then

public class Preferences implements BillingInterface {

解决方案

Java does not support multiple inheritance.

There are a few workarounds I can think of:

The first is aggregation: make a class that takes those two activities as fields.

The second is to use interfaces.

The third is to rethink your design: does it make sense for a Preferences class to be both a PreferenceActivity and an AbstractBillingActivity?

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

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