Redundant generic parameters

后端 未结 6 1870
没有蜡笔的小新
没有蜡笔的小新 2021-01-12 03:36

I have this two interfaces and classes:

public interface Identifiable {
    T getId();
}

public interface GenericRepository

        
6条回答
  •  被撕碎了的回忆
    2021-01-12 04:15

    I don't think you can omit it. With T extends Identifiable you want to say that the generic type parameter must be an Identifiable. Since Identifiable is a generic class, you need to mention its generic type parameter too (if you want to play by the rules that is - if you omit it, you lose all generic type safety for GenericRepository, due to backward compatibility rules). Note also that K is actually used as the parameter type of GenericRepository.get. And since that type may be different from T, you need to satisfy the compiler by declaring it as another generic type parameter of GenericRepository. Otherwise the compiler has no way of knowing what K is.

提交回复
热议问题