Returning an objects subclass with generics
问题 With an abstract class I want to define a method that returns "this" for the subclasses: public abstract class Foo { ... public <T extends Foo> T eat(String eatCake) { ... return this; } } public class CakeEater extends Foo {} I want to be able to do things like: CakeEater phil = new CakeEater(); phil.eat("wacky cake").eat("chocolate cake").eat("banana bread"); Arguably banana bread would throw an IllegalArgumentException with the message "Not a cake!" 回答1: public abstract class Foo<T extends