因为 Java 中的一个类不能扩展多个类。我怎样才能做到这一点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12518972/
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
Since a class in Java cannot extend multiple classes. How would I be able to get by this?
提问by Bdk Fivehunna
I have two classes that need to extend one class. I am getting a compiler error since this cannot happen in Java. I know that you can implement as many interfaces you want to in Java but can only extend one other class. How can I fix this problem?
我有两个类需要扩展一个类。我收到编译器错误,因为这在 Java 中不会发生。我知道您可以在 Java 中实现任意数量的接口,但只能扩展一个其他类。我该如何解决这个问题?
回答by Jeffrey Blattman
Use a "has A" relationship instead of "is An".
使用“has A”关系而不是“is An”。
class A
class B
You (think) you want:
你(认为)你想要:
class C extends A, B
Instead, do this:
相反,请执行以下操作:
class C {
A theA;
B theB;
}
Multiple inheritance is almost always abused. It's not proper to extend classes just as an easy way to import their data and methods. If you extend a class, it should truly be an "is An" relationship.
多重继承几乎总是被滥用。将类扩展为导入其数据和方法的简单方法是不合适的。如果你扩展一个类,它应该真正是一个“是一个”关系。
For example, suppose you had classes,
例如,假设你有课程,
class Bank extends Financial
class Calculator
You might do this if you want to use the functions of the Calculator
in Bank
,
如果您想使用Calculator
in的功能,您可以这样做Bank
,
class Bank extends Calculator, Financial
However, a Bank
is most definitely NOT a Calculator
. A Bank usesa Calculator
, but it isn't one itself. Of course, in java, you cannot do that anyway, but there are other languages where you can.
但是, aBank
绝对不是 a Calculator
。一银行使用一个Calculator
,但它不是一个本身。当然,在 Java 中,无论如何你都不能这样做,但还有其他语言可以。
If you don't buy any of that, and if you REALLY wanted the functions of Calculator
to be part of Bank
's interface, you can do that through Java interfaces.
如果您不购买任何一个,并且您真的希望 的功能Calculator
成为Bank
的接口的一部分,您可以通过 Java 接口来做到这一点。
interface CalculatorIntf {
int add(int a, int b);
}
class Calculator implements CalculatorInf {
int add(int a, int b) { return a + b };
}
class Bank extends Financial implements CalculatorIntf
Calculator c = new Calculator();
@Override // Method from Calculator interface
int add(int a, int b) { c.add(a, b); }
}
A class can implement as many interfaces as it wants. Note that this is still technically a "has A" relationship
一个类可以实现任意数量的接口。请注意,这在技术上仍然是“具有 A”的关系
回答by SJuan76
"Two classes that extend one class" is legal.
“两个类扩展一个类”是合法的。
"One class extending two classes" is against the specification of the language. If you do not want to consider interfaces, it cannot be done.
“一个类扩展两个类”是违反语言规范的。如果您不想考虑接口,则无法完成。
回答by Nandkumar Tekale
To avoid diamond problemJava does not support Multiple Inheritance through classes but it supports using interfaces.
为了避免钻石问题,Java 不支持通过类进行多重继承,但它支持使用接口。
So you may use Association
Relationship. e.g.
所以你可以使用Association
关系。例如
Class A {}
Class B {}
Class C implements SomeInterface {
A a;
B b;
// setter getter for a and b and other methods
}
回答by Aniket Inge
Two classes extending ONE class? like so:
两个类扩展一个类?像这样:
class A{ }
class B extends A{ }
class C extends A{ }
Here, B and C(two classes) extend A(one class). But I am sure you meant One class extending Two separate classes(Multiple Inheritance). WorkAround: you could make a composite Object that has two separate objects(Has-A relationship) like so:
这里,B 和 C(两个类)扩展了 A(一个类)。但我确定你的意思是一个类扩展了两个独立的类(多重继承)。解决方法:您可以制作一个具有两个单独对象(具有-A 关系)的复合对象,如下所示:
class A { }
class B { }
class C extends A { /*define new functionality here */ }
class D extends B { /*define new functionality here */ }
class E { private C cObj; private D dObj; }