Java 一个类可以扩展两个类吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6587621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:59:11  来源:igfitidea点击:

Can one class extend two classes?

javaandroidmultiple-inheritance

提问by LA_

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 AbstractBillingActivitywith Preferences then?

更新。既然这是不可能的,那么我应该如何使用带有首选项的AbstractBillingActivity呢?

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

更新2。如果我使用接口,我应该创建:

  1. BillingInterface

    public interface BillingInterface extends PreferenceActivity, AbstractBillingActivity {
    
    }
    
  2. PreferenceActivity

    public interface PreferenceActivity {
    
    }
    
  3. AbstractBillingActivity

    public interface AbstractBillingActivity {
    
            void onCreate(Bundle savedInstanceState);
    
    }
    
  1. 计费接口

    public interface BillingInterface extends PreferenceActivity, AbstractBillingActivity {
    
    }
    
  2. 偏好活动

    public interface PreferenceActivity {
    
    }
    
  3. 抽象计费活动

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

and then

进而

public class Preferences implements BillingInterface {

采纳答案by Etienne de Martel

Java does not support multiple inheritance.

Java 不支持多重继承。

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 Preferencesclass to be both a PreferenceActivityandan AbstractBillingActivity?

第三是重新思考你的设计:一个Preferences类既是 aPreferenceActivity是 an有意义AbstractBillingActivity吗?

回答by Mat

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

Java 不支持多重继承。您可以实现多个接口,但不能扩展多个类。

回答by Jules

No you cannot make a class extend to two classes.

不,你不能让一个类扩展到两个类。

A possible solution is to make it extend from another class, and make thatclass extend from another again.

一个可能的解决方案是从另一个类扩展,使类从另一个再次扩展。

回答by Paul Sonier

What you're asking about is multiple inheritance, and it's very problematic for a number of reasons. Multiple inheritance was specifically avoided in Java; the choice was made to support multiple interface implementation, instead, which is the appropriate workaround.

您要问的是多重继承,由于多种原因,这很成问题。Java 中特别避免了多重继承;相反,选择支持多个接口实现,这是适当的解决方法。

回答by MD Sayem Ahmed

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

Java 不支持多重继承,这就是为什么你不能同时从两个不同的类扩展一个类。

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

相反,使用单个类来扩展并用于interfaces包含附加功能。

回答by djangofan

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.

Java 1.8(以及 Groovy 和 Scala)有一个叫做“ Interface Defender Methods”的东西,它们是带有预定义默认方法主体的接口。通过实现多个使用防御者方法的接口,您可以在某种程度上有效地扩展两个接口对象的行为。

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:

此外,在 Groovy 中,使用 @Delegate 注释,您可以扩展两个或多个类的行为(当这些类包含同名方法时需要注意)。这段代码证明了这一点:

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

回答by Jonny Rimkus

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

另一种解决方案是创建一个扩展第二个类的私有内部类。例如一个扩展JMenuItem和的类AbstractAction

public class MyClass extends JMenuItem {


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

}

回答by nejckorasa

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

Java 不支持多重继承。但是,您的问题可以使用接口解决。

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

最简单的方法是创建一个接口,用于AbstractBillingActivityPreferenceActivity和实施他们两个。

回答by AyukNayr

Familiar with multilevel hierarchy?

熟悉多级层次结构吗?

You can use subclass as superclass to your another class.

您可以将子类用作另一个类的超类。

You can try this.

你可以试试这个。

public class PreferenceActivity extends AbstractBillingActivity {}

then

然后

public class Preferences extends PreferenceActivity {}

In this case, Preferences class inherits both PreferencesActivity and AbstractBillingActivity as well.

在这种情况下,Preferences 类也继承了 PreferencesActivity 和 AbstractBillingActivity。

回答by CrazySynthax

I can think of a workaround that can help if the classes you want to extend include only methods.

如果您要扩展的类仅包含方法,我可以想到一个可以提供帮助的解决方法。

Write these classes as interfaces. In Java, you can implements any number of interfaces, and implement the methods as default methods in the interfaces.

将这些类编写为接口。在 Java 中,您可以实现任意数量的接口,并将这些方法实现为接口中的默认方法。

https://www.geeksforgeeks.org/default-methods-java/

https://www.geeksforgeeks.org/default-methods-java/