Answers:
您只能扩展一个类。并从许多来源实现接口。
扩展多个类不可用。我能想到的唯一解决方案是不继承任何一个类,而是继承每个类的内部变量,并通过将对对象的请求重定向到您希望它们去的对象来做更多的代理。
public class CustomActivity extends Activity {
private AnotherClass mClass;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mClass = new AnotherClass(this);
}
//Implement each method you want to use.
public String getInfoFromOtherClass()
{
return mClass.getInfoFromOtherClass();
}
}
这是我想出的最好的解决方案。您可以从这两个类中获得功能,并且实际上仍然只能属于一种类类型。
缺点是您无法使用演员表来适应内部类的模具。
为什么不使用内部类(嵌套)
class A extends B {
private class C extends D {
//Classes A , B , C , D accessible here
}
}
Fragment。我的活动必须扩展Fragment,这意味着我绝对需要getView(),如果它在内部类内部,它将无法正常工作,并且其中的代码需要使用from的代码ListActivity,因此我不能扩展两次,或者执行内部类在这个例子中。
正如其他人所说。不,你不能。但是,尽管多年来人们多次说过您应该使用多个接口,但他们并没有真正考虑如何使用。希望这会有所帮助。
假设您拥有class Foo并且class Bar都想尝试扩展为class FooBar。当然,正如您所说,您不能:
public class FooBar extends Foo, Bar
人们已经在某种程度上了解了其原因。而是interfaces为两者编写Foo并Bar覆盖其所有公共方法。例如
public interface FooInterface {
public void methodA();
public int methodB();
//...
}
public interface BarInterface {
public int methodC(int i);
//...
}
现在制作Foo并Bar实现相关接口:
public class Foo implements FooInterface { /*...*/ }
public class Bar implements BarInterface { /*...*/ }
现在,使用class FooBar,您可以实现FooInterface和,BarInterface同时保留Foo和Bar对象,并直接将方法直接传递给:
public class FooBar implements FooInterface, BarInterface {
Foo myFoo;
Bar myBar;
// You can have the FooBar constructor require the arguments for both
// the Foo and the Bar constructors
public FooBar(int x, int y, int z){
myFoo = new Foo(x);
myBar = new Bar(y, z);
}
// Or have the Foo and Bar objects passed right in
public FooBar(Foo newFoo, Bar newBar){
myFoo = newFoo;
myBar = newBar;
}
public void methodA(){
myFoo.methodA();
}
public int methodB(){
return myFoo.methodB();
}
public int methodC(int i){
return myBar.methodC(i);
}
//...
}
此方法的好处,是该FooBar对象适合两者的模具FooInterface和BarInterface。这意味着这很好:
FooInterface testFoo;
testFoo = new FooBar(a, b, c);
testFoo = new Foo(a);
BarInterface testBar;
testBar = new FooBar(a, b, c);
testBar = new Bar(b, c);
希望这阐明了如何使用接口而不是多个扩展。即使我迟到了几年。
您将要使用接口。通常,由于钻石问题,多重继承是不好的:
abstract class A {
abstract string foo();
}
class B extends A {
string foo () { return "bar"; }
}
class C extends A {
string foo() {return "baz"; }
}
class D extends B, C {
string foo() { return super.foo(); } //What do I do? Which method should I call?
}
C ++和其他人有几种解决方法,例如
string foo() { return B::foo(); }
但是Java只使用接口。
Java Trail对接口进行了很好的介绍:http : //download.oracle.com/javase/tutorial/java/concepts/interface.html 在深入研究Android API中的细微差别之前,您可能需要遵循它。
virtual class。这样做会给编译器和开发人员带来许多警告和警告。
super在继承的方法中调用。可以,但是您必须指定要调用的接口方法(并且必须default在接口实现中使用关键字)。一个示例是:MyIFace.super.foo()其中MyIFace是接口。如您所见,已定义要执行的接口方法,并且完全避免了Diamond问题。如果您扩展MyClass1和MyClass2,则两个类都有一个foo()和调用,super.foo()则Diamond问题会引发编译器。
"bar"或"baz"方法类型为void。无论如何,很好的解释xD
是的,正如其他人所写的那样,您不能在Java中进行多重继承。如果您有两个要使用代码的类,则通常只将其子类化(例如class A)。对于类B,您可以将其的重要方法抽象到一个接口上BInterface(丑陋的名字,但是您有主意),然后说Main extends A implements BInterface。在内部,您可以实例化类的对象B并BInterface通过调用的相应函数来实现的所有方法B。
由于您Main现在是A,但具有,因此会将“是”关系更改为“有”关系B。根据您的用例,您甚至可以通过BInterface从A类中删除来进行显式更改,而是提供一种直接访问B对象的方法。
制作一个界面。Java没有多重继承。
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html
Java不支持多重继承,但是您可以尝试实现两个或多个接口。
像另一种替代方法一样,也许您可以将接口与方法的默认实现一起使用。当然,这取决于您要做什么。
例如,您可以创建一个抽象类和一个接口:
public abstract class FatherClass {
abstract void methodInherit() {
//... do something
}
}
public interface InterfaceWithDefaultsMethods {
default void anotherMethod() {
//... do something
//... maybe a method with a callable for call another function.
}
}
因此,在那之后,您可以扩展和实现这两个类并使用这两种方法。
public class extends FatherClass implements InterfaceWithDefaultsMethods {
void methode() {
methodInherit();
anotherMethod();
}
}
希望这对您有帮助...
您不能在Java中进行多重继承。考虑使用接口:
interface I1 {}
interface I2 {}
class C implements I1, I2 {}
或内部类:
class Outer {
class Inner1 extends Class1 {}
class Inner2 extends Class2 {}
}