我有一些类,像这样:Java继承和泛型
模型
public abstract class BaseEntity<O extends Object> { ... }
public class Person extends BaseEntity<Person> { ... }
COMMAND
public abstract class BaseCommand<BE extends BaseEntity<BE>> { ... }
public class PersonCommand extends BaseCommand<Person> { ... }
服务
public interface BaseService<BE extends BaseEntity<BE>> {
public BE create(BaseCommand<BE> command);
}
public interface PersonService extends BaseService<Person> { ... }
服务IMPL
public abstract class BaseServiceImpl<BE extends BaseEntity<BE>> implements BaseService<BE> { }
public class PersonServiceImpl extends BaseServiceImpl<Person> implements PersonService {
public Person create(PersonCommand personCommand) { ... }
}
的PersonServiceImpl
类将无法编译。它不认识到create()
方法正在实施BaseService
接口的create()
方法。任何人都可以告诉为什么PersonCommand
未被识别为BaseCommand<BE>
(在参数列表中)?
我不明白你为什么要用自己参数化类... public class Person extends BaseEntity {...} –
I82Much
他们没有相同的返回类型也许? – talnicolas
基本上我这样做,以便BaseEntity中的方法可以引用该类型。公共BE方法(); etc –