我有一个要扩展的构建器类,这是简单的版本:
class A {
public A withSomeAStuff() {
return this;
}
}
A a = new A().withSomeAStuff();
当我扩展它时,我知道我可以毫无问题地做到这一点:
class AA<T extends AA> {
public T withSomeAStuff() {
return (T) this;
}
}
class BB extends AA<BB> {
public BB withSomeBStuff() {
return this;
}
}
AA aa = new AA().withSomeAStuff();
BB bb = new BB().withSomeAStuff().withSomeBStuff();
但现在我想用另一个类进一步扩展它,所以我试试这个:
class AAA<T extends AAA> {
public T withSomeAStuff() {
return (T) this;
}
}
class BBB<T extends BBB> extends AAA<T> {
public T withSomeBStuff() {
return (T) this;
}
}
class CCC extends BBB<CCC> {
public CCC withSomeCStuff() {
return this;
}
}
AAA aaa = new AAA().withSomeAStuff();
BBB bbb = new BBB().withSomeAStuff().withSomeBStuff(); //breaks here!
CCC ccc = new CCC().withSomeAStuff().withSomeBStuff().withSomeCStuff();
我的新 CCC 课程运行良好,但我的 BBB 课程坏了,我不知道为什么。
我需要做什么来修复它?