这是你要找的吗?(这应该在评论中,因为我不确定你在寻找什么,但它并没有很好地显示在那里。)
interface A {
void foo();
void baz();
}
class B extends A{
void foo(){/* B's impl. (must be impl.)*/};
void baz(){/* B's impl. (must be impl.)*/};
}
class C extends A {
void foo(){/* C's impl. (must be impl.)*/};
void baz(){/* C's impl. (must be impl.)*/};
}
class D extends C {
void foo(){/* D's impl */};
void baz(){/* D's impl, if not included C's impl will be used */};
}
class E extends B {
void foo(){/* E's impl, if not included B's impl will be used.*/};
void baz(){/* E's impl, if not included B's impl will be used.*/};
}
或者,如果您希望 B 和 C 都从 A 共享方法,您可以这样做...
class A {
void foo(){ /*(must be impl.)*/ };
void baz(){ /*(must be impl.)*/ };
}
class B extends A {
void foo(){/* B's impl, if not included A's impl will be used*/};
void baz(){/* B's impl, if not included A's impl will be used*/};
}
class C extends A {
void foo(){/* C's impl, if not included A's impl will be used*/};
void baz(){/* C's impl, if not included A's impl will be used*/};
}
class D extends C {
void foo(){/* D's impl, if not included C's impl will be used */};
void baz(){/* D's impl, if not included C's impl will be used */};
}
class E extends B {
void foo(){/* E's impl, if not included B's impl will be used.*/};
void baz(){/* E's impl, if not included B's impl will be used.*/};
}
如果你想让 A 实现 foo(),而不是 baz(),你可以让它抽象,像这样......
abstract class A {
void foo(){ /*(must be impl.)*/ };
abstract void baz();
}
class B extends A{
void foo(){/* B's impl, if not included A's impl will be used*/};
void baz(){ /*(must be impl.)*/ };
}
class C extends A {
void foo(){/* C's impl, if not included A's impl will be used*/};
void baz(){ /*(must be impl.)*/ };
}
class D extends C {
void foo(){/* D's impl, if not included C's impl will be used */};
void baz(){/* D's impl, if not included C's impl will be used */};
}
class E extends B {
void foo(){/* E's impl, if not included B's impl will be used.*/};
void baz(){/* E's impl, if not included B's impl will be used.*/};
}