1 回答
TA贡献1802条经验 获得超4个赞
我会将所有常见方法移至A1另一个抽象级别(我们称之为ABase),并声明A1它们A2的特殊行为(如果有)。
这样,如果C3添加更多相同类型的类,则不需要再次重新定义methodA。
public interface I1 {
public void methodA();
}
public abstract class ABase implements I1 {
//other methods
}
public abstract class A1 extends ABase {
public void methodA(){
//default behaviour
}
}
public abstract class A2 extends ABase {
public void methodA(){
//default behaviour
}
}
public class C1 extends A1 {
//do nothing regarding methodA, because the default behaviour in A1 is desired
//other methods
}
public class C2 extends A1 {
//do nothing regarding methodA, because the default behaviour in A1 is desired
//other methods
}
public class C3 extends A2 {
//do nothing regarding methodA, because the default behaviour in A2 is desired
//other methods
}
添加回答
举报