Can one class extend two classes?

2020-01-29 04:04发布

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 {

13条回答
爷的心禁止访问
2楼-- · 2020-01-29 04:13

Java does not support multiple inheritance. However, your problem may be solved using interfaces.

The easiest solution would be to create an interface for AbstractBillingActivity and PreferenceActivityand implement them both.

查看更多
相关推荐>>
3楼-- · 2020-01-29 04:15

Also, instead of inner classes, you can use your 2 or more classes as fields.

For example:

Class Man{
private Phone ownPhone;
private DeviceInfo info;
//sets; gets
}
Class Phone{
private String phoneType;
private Long phoneNumber;
//sets; gets
}

Class DeviceInfo{

String phoneModel;
String cellPhoneOs;
String osVersion;
String phoneRam;
//sets; gets

}

So, here you have a man who can have some Phone with its number and type, also you have DeviceInfo for that Phone.

Also, it's possible is better to use DeviceInfo as a field into Phone class, like

class Phone {
DeviceInfo info;
String phoneNumber;
Stryng phoneType;
//sets; gets
}
查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-01-29 04:16

Another solution is to create a private inner class that extends the second class. e.g a class that extends JMenuItem and AbstractAction:

public class MyClass extends JMenuItem {


    private class MyAction extends AbstractAction {
        // This class can access everything from its parent...
    }

}
查看更多
家丑人穷心不美
5楼-- · 2020-01-29 04:21

Java doesn't support multiple inheritance. You can implement multiple interfaces, but not extend multiple classes.

查看更多
干净又极端
6楼-- · 2020-01-29 04:21

Java does not support multiple inheritance, that's why you can't extend a class from two different classes at the same time.

Rather, use a single class to extend from, and use interfaces to include additional functionality.

查看更多
爷的心禁止访问
7楼-- · 2020-01-29 04:24

Java 1.8 (as well as Groovy and Scala) has a thing called "Interface Defender Methods", which are interfaces with pre-defined default method bodies. By implementing multiple interfaces that use defender methods, you could effectively, in a way, extend the behavior of two interface objects.

Also, in Groovy, using the @Delegate annotation, you can extend behavior of two or more classes (with caveats when those classes contain methods of the same name). This code proves it:

class Photo {
    int width
    int height
}    
class Selection {
    @Delegate Photo photo    
    String title
    String caption
}    
def photo = new Photo(width: 640, height: 480)
def selection = new Selection(title: "Groovy", caption: "Groovy", photo: photo)
assert selection.title == "Groovy"
assert selection.caption == "Groovy"    
assert selection.width == 640
assert selection.height == 480
查看更多
登录 后发表回答