如何引用泛型类型参数的泛型类型参数?

时间:2014-06-17 06:15:20

标签: java generics compiler-errors

实体界面:

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。

1 个答案:

答案 0 :(得分:1)

不可能。

Dao<T extends Entity<Id>, Id>第二类型参数Id中的

Dao参数化两个类型参数,一个有界类型T和其他Id的意义上并不冗余此外,EntityId上进行参数化。 (见差异,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);
}
相关问题