java - 为什么泛型绑定(bind) "E implements I"会导致编译器错误?

标签 java generics

为什么编译:

class MaxMin<E extends Comparable<E>>
{
   E max=null;
   E min=null;
}

...但这不是吗?

class MaxMin<E implements Comparable<E>>
{
   E max=null;
   E min=null;
}

最佳答案

通用类型边界仅指定extendssuper

引用Java Generics Tutorial (强调我的)

To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound, which in this example is Number. Note that, in this context, extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces)

关于java - 为什么泛型绑定(bind) "E implements I"会导致编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8029214/

相关文章:

java - 在 Gson 自定义序列化中为 Double 字段分配空值

Java 和泛型的使用?

Java 自定义通用列表 : add() method error

java - Java代码注释中使用方括号的目的是什么?

java - 给定两个不包括周末的日期计算日期

java - 在 Java 中设置哈希表数组

Java 类路径和类加载

java - 使用mockito 来模拟具有通用方法的服务

c# - 将 BindingSource.DataSource 声明为通用的

java 泛型 <T extends Number>