Java 一个类可以有多个超类吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23207693/
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-13 21:20:16  来源:igfitidea点击:

Is it possible to have a class have more than one superclass?

javaswinginheritancesubclasssuperclass

提问by Ungeheuer

I am creating an incredibly primitive blackHyman game for my high school programming class using Swing, therefore, all my resource classes extend JPanel. I created the following resource classes:

我正在使用 Swing 为我的高中编程课创建一个非常原始的二十一点游戏,因此,我所有的资源类都扩展了 JPanel。我创建了以下资源类:

  • DealerBox
  • ClubsBox
  • SpadesBox
  • HeartsBox
  • 经销商箱
  • 俱乐部盒子
  • 黑桃盒
  • 心盒

My problem is that I want to create a handful of public integers such that I have

我的问题是我想创建一些公共整数,以便我有

public int DealerTotal, ClubsTotal, SpadesTotal, HeartsTotal;

I want to do this so I can track the values of everybody's hands and determine winners and losers. I had a lightbulb go off and thought that making the Dealerbox a superclass to Clubs, Spades, and Hearts was the solution to all my troubles as I could have simple code in the superclass getting values for the above integers and do all the checking I need.

我想这样做,以便我可以跟踪每个人的牌值并确定赢家和输家。我的灯泡熄灭了,我认为使 Dealerbox 成为 Clubs、Spades 和 Hearts 的超类是解决我所有麻烦的方法,因为我可以在超类中使用简单的代码获取上述整数的值并进行所有我需要的检查.

So I wrote this for one of my subclasses:

所以我为我的一个子类写了这个:

public class ClubsBox extends JPanel, DealerBox implements Runnable
{
//fun code 
}

The problem now is that I keep getting an error. I assumed it had something to do with listing ClubsBox as being the subclass of JPanel AND DealerBox. So I stackoverflowed my problem and found an answer that stated that one cannot have a class that has 2 superclasses. Another answer to a similar question said that one COULD have a class that has two superclasses.

现在的问题是我不断收到错误消息。我认为这与将 ClubsBox 列为 JPanel AND DealerBox 的子类有关。所以我计算了我的问题并找到了一个答案,指出一个人不能有一个具有 2 个超类的类。对类似问题的另一个答案是,一个类可以有一个具有两个超类的类。

Now we get to the main question: Is it possible to have a class that has two superclasses? If so, how do I code it? If not, is there a way around the restriction?

现在我们进入主要问题:是否可能有一个具有两个超类的类?如果是这样,我该如何编码?如果没有,有没有办法绕过限制?

EDIT: 21 APRIL 2014, 44 MINUTES AFTER ORIGINAL POST

编辑:2014 年 4 月 21 日,原始帖子后 44 分钟

I have a new problem. I used the method that everyone suggested which was to make DealerBox extend JPanel then to have everything else extend DealerBox. My GUI went from this: GUI before chaining

我有一个新问题。我使用了每个人都建议的方法,即让 DealerBox 扩展 JPanel,然后让其他所有内容扩展 DealerBox。我的 GUI 来自于: 链接前的 GUI

To This:

对此:

GUI after chainingThe bottom one is clearly incredibly screwed up. Somehow labels from other areas ended up being copied and stuff got mixed around. If you need the code let me know, but I need serious help fixing this.

链接后的 GUI底部的一个显然是令人难以置信地搞砸了。不知何故,来自其他地区的标签最终被复制,东西混杂在一起。如果您需要代码,请告诉我,但我需要认真的帮助来解决这个问题。

Please keep in mind the inheritance tree looks like this:

请记住继承树看起来像这样:

InhertianceTree

继承树

采纳答案by Victor R. Oliveira

When Sun was designing Java, it omitted multiple inheritance - or more precisely multiple implementation inheritance - on purpose. Yet multiple inheritance can be useful, particularly when the potential ancestors of a class have orthogonal concerns. This article presents a utility class that not only allows multiple inheritance to be simulated, but also has other far-reaching applications.

Sun 在设计 Java 时,故意省略了多重继承——或者更准确地说是多重实现继承。然而多重继承可能很有用,特别是当一个类的潜在祖先具有正交关注点时。本文介绍了一个实用程序类,它不仅允许模拟多重继承,而且还有其他影响深远的应用。

Have you ever found yourself wanting to write something similar to:

你有没有发现自己想写一些类似的东西:

public class Employee extends Person, Employment {
// detail omitted
}

Here, Person is a concrete class that represents a person, while Employment is another concrete class that represents the details of a person who is employed. If you could only put them together, you would have everything necessary to define and implement an Employee class. Except in Java - you can't. Inheriting implementation from more than one superclass - multiple implementation inheritance - is not a feature of the language. Java allows a class to have a single superclass and no more.

这里,Person 是一个具体的类,代表一个人,而 Employment 是另一个具体的类,代表一个人的详细信息。如果您只能将它们放在一起,您将拥有定义和实现 Employee 类所需的一切。除了在 Java 中 - 你不能。从多个超类继承实现 - 多个实现继承 - 不是该语言的特性。Java 允许一个类只有一个超类,仅此而已。

For futher information read http://www2.sys-con.com/itsg/virtualcd/java/archives/0810/haywood/index.html

有关更多信息,请阅读http://www2.sys-con.com/itsg/virtualcd/java/archives/0810/haywood/index.html

One option to you is "Inheritance from Inheritance":

您的一种选择是“从继承中继承”:

public class Person extends Employee
public class Employee extends Employment

回答by SimonT

Unfortunately you can't have more than one direct superclass in Java. But you can chain them together like this:

不幸的是,在 Java 中不能有多个直接超类。但是你可以像这样将它们链接在一起:

public class DealerBox extends JPanel
public class ClubsBox extends DealerBox

Now with the new issue of your components not being laid out in the same manner as they were before, it's too hard to guess at what's going on without seeing some code. Are you putting common code into the constructor for DealerBox, and calling it from each DealerBoxsubclass? Like so:

现在,您的组件的新问题不像以前那样以相同的方式布置,如果不看一些代码就很难猜测发生了什么。您是否将公共代码放入 for 的构造函数中DealerBox,并从每个DealerBox子类调用它?像这样:

public class DealerBox extends JPanel {
    public DealerBox(String info) {
        super(); // JPanel constructor
        doSomething();
    }
}

public class ClubsBox extends DealerBox {
    public ClubsBox() {
        super("Clubs"); // DealerBox constructor
    }
}

回答by isak gilbert

Multiple inheritance is not allowed in Java. So you cannot have two superclasses.

Java 中不允许多重继承。所以你不能有两个超类。

The way to get around this would have ClubsBox extend only DealerBox and then DealerBox itself extends JPanel.

解决这个问题的方法是让 ClubsBox 只扩展 DealerBox,然后 DealerBox 本身扩展 JPanel。

ClubsBox:

俱乐部盒子:

public class ClubsBox extends DealerBox implements Runnable {

}

DealerBox:

经销商箱:

public class DealerBox extends JPanel {

}

回答by werkn

Classesin Java can only extend one class, your trying to extend two. They can; however, implement multiple interfaces.

Java 中的只能扩展一个类,您尝试扩展两个类。他们能; 但是,实现多个interfaces.

You can have multiple inheritance though, something like this:

你可以有多重继承,就像这样:

Class Integer extends Number ...
Class BigInteger extends Integer ...
Class ReallyBigInteger extends BigInteger ...

and so on and so forth, does this help?

等等等等,这有帮助吗?

回答by rahul uday

No Java doesn't allow you to have more than one Super class or inherit more than one Parent class. This concept is called as multiple inheritance.The reason behind is the diamond problem to know more about multiple inheritance and Diamond problem check: https://www.geeksforgeeks.org/java-and-multiple-inheritance/

没有 Java 不允许您拥有多个超类或继承多个父类。这个概念被称为多重继承。 背后的原因是钻石问题,更多地了解多重继承和钻石问题检查:https: //www.geeksforgeeks.org/java-and-multiple-inheritance/

回答by Pratham Sarankar

In Java, a child class cannot have more than one parent class. But what you want is be achievable, you can create a package, have classes in that package. And in the package where your current class is, you have to import that package. But remember, you have to keep the 'public' as access specifier.

在 Java 中,一个子类不能有多个父类。但是你想要的是可以实现的,你可以创建一个包,在那个包中有类。在当前类所在的包中,您必须导入该包。但请记住,您必须保留“public”作为访问说明符。