使用Java中的Comparable接口进行类型边界

huangapple go评论78阅读模式
英文:

Type Bounds with Comparable Interface in Java

问题

在JDK 11中,我正在创建一个名为“Collection”的泛型类,以便无论提供哪种类型的类型参数,该类型参数都必须实现Comparable接口。当前的类声明如下:

public class Collection<T extends Comparable<T>>

最初,我认为类声明应该是这样的:

public class Collection<T implements Comparable<T>>

但JDK不喜欢那样做。

因此,我的问题是为什么类声明是前者,而不是后者。我们不是应该扩展类和实现接口吗?

英文:

In JDK 11 I'm creating a generic class called "Collection" such that whatever type is provided for the type parameter must implement the Comparable interface. The class declaration is currently as follows:

public class Collection &lt;T extends Comparable&lt;T&gt;&gt; 

Originally I thought the class declaration should be this:

public class Collection &lt;T implements Comparable&lt;T&gt;&gt; 

but JDK didn't like that.

So my question is why the class declaration is the former rather than the latter. Don't we extend classes and implement interfaces?

答案1

得分: 3

关于边界声明的语法,为了在Java 5中引入泛型的语言设计师们面临着以下选择:

  • 是否保留声明结构(使用独立的extendsimplements边界),或者
  • 选择一种形式来表示上界类型(例如,T extends FooT <: Foo等)

后者(在我看来很明智)被认为是更加务实的选择,因为在声明类型变量时,超类型是类还是接口并不重要,而且使用需要精确关键字的繁琐语法(会使复合边界如<T extends ArrayList implements Serializable>变得更加丑陋)可能只会让使用变得更加困难,对于代码的编写者和读者都没有实际好处。

在语言不断演进的过程中,这样的决策经常会出现,严格遵循先例可能只会使事情变得更加困难,没有实际好处,因此语言设计师有时会(在仔细考虑了两种选择的利弊之后)选择打破先例,以获得更好的结果。

英文:

With respect to the syntax of bounds declaration, the language architects who added generics in Java 5 faced the following choice:

  • Mirror the declaration structure (with separate extends and implements bounds), or
  • Choose one form for upper-bounded types (e.g., T extends Foo, T &lt;: Foo, etc.)

The latter was (sensibly, IMO) viewed as the more pragmatic choice, as at the point of declaring a type variable, whether the supertype happens to be a class or an interface is not material, and having a fussy syntax that required clients to use exactly the right keyword (and would have made compound bounds like &lt;T extends ArrayList implements Serializable> even uglier) would likely be viewed as just making things harder to use with no actual benefit to either writers or readers of code.

Such decision often come about as a language is evolved, where strict consistency with precedent would only make things more difficult for no good benefit, and so language designers sometimes (after carefully considering the pros and cons of both alternatives) choose to break with precedent for the sake of a better result.

答案2

得分: 0

  1. 集合已经在java.util中存在。创建另一个同名类会让大家感到困惑;我建议你选择其他名称。

  2. 泛型是关于类型的。使用了extends这个词,这在某种程度上类似于在例如class Foo extends Bar中使用该关键字的方式,但意义不完全相同。关键在于,规范指出是使用extends而不是implements,无论之后是什么内容,对于这一点没有必要深究得比'规范就是这样'更进一步。

英文:
  1. Collection is already in java.util. Creating another class with that name is going to confuse everybody; I suggest you pick something else.

  2. Generics is about types in general. The word extends is used, and this kinda mirrors the same use of that keyword in, for example, class Foo extends Bar, but it's not quite the same meaning. The point is, the spec says it is extends and not implements regardless of what comes after, and there's not much point in going any further than 'spec says so' on this one.

答案3

得分: 0

因此,您是否希望使用 implements E 还是 extends E,考虑到 E 可能是类或接口?除非您想要第三个关键字(可能是上下文敏感的),否则区分这两者就没有意义。

英文:

Would you therefore want implements E or extends E, given that E may be a class or an interface? Unless you want a third keyword (possibly context-sensitive) then it doesn't make sense to distinguish.

huangapple
  • 本文由 发表于 2020年3月15日 10:23:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/60689167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定