实体界面:
public interface Entity<Id>
{
Id getId();
}
和道:
public interface Dao<T extends Entity<Id>, Id>
{
T find(Id id);
}
如果我尝试删除Dao(Id)上的第二个类型参数,我得到&#34; Id无法解析为类型&#34;。我的问题是,它是否有可能摆脱Dao上的第二个类型参数,因为它本质上是多余的。
为了清楚起见,我试图避免在我使用Dao的任何地方重复Id类型。在Entity接口中指定一次类型就足够了。
现在我必须像这样重复自己:
private Dao<Car, UUID> carDb;
我到处都在使用Car Dao。
答案 0 :(得分:1)
不可能。
Dao<T extends Entity<Id>, Id>
第二类型参数Id
中的在Dao
参数化两个类型参数,一个有界类型T
和其他Id
的意义上并不冗余此外,Entity
在Id
上进行参数化。 (见差异,by this you are restricting the type parameter of Entity to be same as second type parameter of Dao
)
只有在编译器已经知道实体的Type参数的情况下才有可能。
interface Dao<T extends Entity<String>> {
T find(String id);
}