Joh*_*eed 1 java oop android design-patterns multiple-inheritance
我已经创建了一个类,其中包含一个带有我的应用程序徽标的自定义标题栏.这种方法很有效,但对于我的大多数类,我需要能够继承该功能以及ListActivity的功能.该怎么办?
任何帮助赞赏.
你应该赞成组合(和委托)而不是继承:
public interface FirstClassInterface {
void method1();
}
public interface SecondClassInterface {
void method2();
}
public class FirstClass implements FirstClassInterface {
// ...
}
public class SecondClass implements SecondClassInterface {
// ...
}
public class FirstAndSecondClass implements FirstClassInterface , SecondClassInterface
{
private FirstClassInterface firstclass;
private SecondClassInterface secondclass;
public FirstAndSecondClass(FirstClassInterface firstclassinterface, SecondClassInterface secondclassinterface) {
this.firstclass= firstclassinterface;
this.secondclass= secondclassinterface;
}
public void method1() {
this.firstclass.method1();
}
public void method2() {
this.secondclass.method2();
}
public static void main(String[] args) {
FirstAndSecondClass t = new FirstAndSecondClass(new FirstClass(), new SecondClass());
t.method1();
t.method2();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1006 次 |
| 最近记录: |