使用抽象类,我想定义一个为子类返回“this”的方法:
public abstract class Foo {
...
public <T extends Foo> T eat(String eatCake) {
...
return this;
}
}
public class CakeEater extends Foo {}
我希望能够执行以下操作:
CakeEater phil = new CakeEater();
phil.eat("wacky cake").eat("chocolate cake").eat("banana bread");
可以说香蕉面包会抛出 IllegalArgumentException 并显示消息“不是蛋糕!”