从两个类扩展

问题描述 投票:0回答:13

我该怎么做:

public class Main extends ListActivity , ControlMenu 

另外,我想知道这种方法是否可以,我已经在类中制作了 ControlMenu 菜单,并且我正在其余的活动中进行扩展。

java android
13个回答
200
投票

您只能扩展一个类。并实现来自许多来源的接口。

无法扩展多个类。我能想到的唯一解决方案是不继承任何一个类,而是拥有每个类的内部变量,并通过将对象的请求重定向到您希望它们转到的对象来执行更多代理操作。

 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();
     }
 }

这是我想出的最好的解决方案。 您可以从两个类中获取功能,但实际上仍然只能属于一种类类型。

缺点是你无法使用强制转换来适应内部类的模具。


77
投票

正如其他人所说。不,你不能。然而,尽管人们多年来多次说过应该使用多个接口,但他们并没有真正讨论如何使用。希望这会有所帮助。

假设您有

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);

希望这能够阐明如何使用接口而不是多个扩展。即使我晚了几年。


58
投票

为什么不使用内部类(嵌套)

class A extends B {
    private class C extends D {
        //Classes A , B , C , D accessible here 
    }
}

39
投票

您将需要使用界面。一般来说,由于钻石问题,多重继承是不好的:

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 的细微差别之前,您可能需要遵循这一点。


22
投票

是的,正如其他人所写的,你不能在 Java 中进行多重继承。 如果您想使用两个类中的代码,通常只需将其中一个子类化(例如类

A
)。对于类
B
,您将其重要方法抽象为接口
BInterface
(名字很丑,但你明白了),然后说
Main extends A implements BInterface
。在里面,你可以实例化一个
B
类的对象,并通过调用
BInterface
对应的函数来实现
B
的所有方法。

这会将“is-a”关系更改为“has-a”关系,因为您的

Main
现在是
A
,但有一个
B
。根据您的用例,您甚至可以通过从
BInterface
类中删除
A
来明确更改,而是提供直接访问 B 对象的方法。


14
投票

制作一个界面。 Java没有多重继承。

http://csis.pace.edu/~bergin/patterns/multipleinheritance.html


6
投票

Java不支持多重继承,但你可以尝试实现两个或多个接口。


6
投票

与另一种选择一样,也许您可以使用具有方法的默认实现的接口。 这当然取决于您想做什么。

例如,您可以创建一个抽象类和一个接口:

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 ChildClassWithInterface extends FatherClass implements InterfaceWithDefaultsMethods {

    void method() {
        methodInherit();
        anotherMethod();
    }
}

希望这对您有帮助...


4
投票

是的。斯兰道是对的。 Java 不允许从多个类扩展。

您想要的可能是

public class Main extends ListActivity implements ControlMenu
。我猜你正在尝试列一个清单。

希望有帮助。


2
投票

Java 中不允许从多个类进行扩展。 为了防止死亡钻石


2
投票

java的创建者认为多重继承的问题超过了好处,因此他们没有包含多重继承。您可以在here了解多重继承的最大问题之一(双菱形问题)。

两个最相似的概念是接口实现和包含其他类的对象作为当前类的成员。在接口中使用默认方法几乎与多重继承完全相同,但是使用仅具有默认方法的接口被认为是不好的做法。


1
投票

有可能

public class ParallaxViewController<T extends View & Parallaxor> extends ParallaxController<T> implements AbsListView.OnScrollListener {

//blah
}

1
投票

在java中你不能进行多重继承。考虑使用接口:

interface I1 {}
interface I2 {}
class C implements I1, I2 {}

或内部类:

class Outer {
    class Inner1 extends Class1 {}
    class Inner2 extends Class2 {}
}
© www.soinside.com 2019 - 2024. All rights reserved.