实体接口:
public interface Entity<Id>
{
Id getId();
}
与道:
public interface Dao<T extends Entity<Id>, Id>
{
T find(Id id);
}
如果我试图删除Dao (Id)上的第二个类型参数,我会得到"Id不能解析为类型"。我的问题是,是否有可能去掉Dao上的第二个类型参数,因为它本质上是冗余的。
要清楚,我试图避免在使用Dao的任何地方重复Id类型。只要在Entity接口中指定一次该类型就足够了。
现在我必须重复一遍:
private Dao<Car, UUID> carDb;
所有我使用Car Dao的地方
这是不可能的。
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
)
只有在编译器已经知道Entity的Type参数的情况下才有可能。
interface Dao<T extends Entity<String>> {
T find(String id);
}