The normal way to do that is introduce a generic parameter:
(通常的方法是引入一个通用参数:)
abstract class Super<Sub extends Super> {
abstract void a_method(Sub param);
}
class Sub_A extends Super<Sub_A> {
void a_method(Sub_A param) {
}
}
class Sub_B extends Super<Sub_B> {
void a_method(Sub_B param) {
}
}
As far as I know you cannot force the subclass to pass themselves as T
, eg it would be possible to define class Sub_C extends Super<Sub_A>
.
(据我所知,您不能强制子类将自身作为T
传递,例如,可以定义class Sub_C extends Super<Sub_A>
。)
Not sure if that is a problem. (不知道这是否是一个问题。)
Following java naming conventions it should look like:
(遵循Java命名约定,它应类似于:)
abstract class Super<S extends Super> {
abstract void aMethod(S param);
}
class SubA extends Super<SubA> {
void aMethod(SubA param) {
}
}
class SubB extends Super<SubB> {
void aMethod(SubB param) {
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…