从两个类扩展
我该如何做到这一点:
public class Main extends ListActivity , ControlMenu 此外,我想知道,这种方法是好的,我已经做了ControlMenu类的菜单,我在其余的活动延伸。
你只能扩展一个class级。 并从多个来源实现接口。
扩展多个类不可用。 我能想到的唯一解决scheme不是inheritance任何一个类,而是拥有每个类的内部variables,并通过将请求redirect到您希望它们转到的对象来执行更多的代理。
  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(); } } 
这是我提出的最好的解决scheme。 你可以从这两个类中获得function,而且实际上只有一个类的types。
缺点是你不能使用cast来适应内部类的模子。
为什么不使用内部类(嵌套)
 class A extends B { private class C extends D { //Classes A , B , C , D accessible here } } 
你会想要使用接口。 一般来说,由于Diamond问题,多重inheritance是不好的:
 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 Trails在接口方面有很好的介绍: http : //download.oracle.com/javase/tutorial/java/concepts/interface.html在深入了解Android API的细微差别之前,您可能会想要遵循这一点。
正如其他人所说的。 不,你不能。 然而,即使人们多年来多次说过,你应该使用多个接口,他们并没有真正进入如何。 希望这会有所帮助。
 假设你有class Foo和class Bar ,你都想尝试扩展到class FooBar 。 当然,正如你所说,你不能这样做: 
 public class FooBar extends Foo, Bar 
 人们已经在一定程度上解释了这个原因。 而是为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); 
希望这个澄清如何使用接口,而不是多个扩展。 即使我迟了几年。
 是的,正如其他人所写,你不能在Java中进行多重inheritance。 如果你有两个你想使用代码的类,你通常只需要一个子类(比如说A类)。 对于B类,你把它的重要方法抽象到一个接口BInterface (丑陋的名字,但你明白了),然后说Main extends A implements BInterface 。 在里面,你可以实例化一个B类的对象,并通过调用B的相应function来实现BInterface的所有方法。 
 这会将“is-a”关系更改为“has-a”关系,因为您的Main现在是A ,但是具有B 根据您的使用情况,您甚至可以通过从A类中删除BInterface来显式更改,而是提供一种直接访问B对象的方法。 
做一个界面。 Java没有多重inheritance。
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html
Java不支持多重inheritance,但可以尝试实现两个或更多接口。
是。 slandau是对的。 Java不允许从几个类扩展。
 你想要的可能是public class Main extends ListActivity implements ControlMenu 。 我猜你正在尝试做一个清单。 
希望有所帮助。
java中不允许扩展多个类,以防止致命的钻石死亡 !
有可能的
 public class ParallaxViewController<T extends View & Parallaxor> extends ParallaxController<T> implements AbsListView.OnScrollListener { //blah } 
就像另一种select,也许你可以使用一个接口和一个方法的默认实现。 这取决于你想要做什么。
例如,您可以创build一个抽象类和一个接口:
 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(); } } 
希望这可以帮助你…