T” 和 “T” 在 JavaDocs 中作为返回类型有什么区别。

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

What is the difference between "<T> T" and "T" return types in JavaDocs

问题

仍在努力理解泛型,因此需要帮助,将不胜感激。

英文:

Still wrapping my head around generics so help would be appreciated.

答案1

得分: 8

在这两种情况下,返回类型都是 T

如果你在之前看到了 &lt;T&gt;,那么意味着泛型类型 T 已在方法级别进行了定义:

&lt;T extends JustAnExample&gt; T getThatThing() {
  // ...
}

如果没有的话,那么可能是在类级别进行了定义:

class MyClass&lt;T extends JustAnExample&gt; {
  T getThatThing() {
    // ...
  }
}

或者,从技术上讲,它也可以只是一个名为 T 的类,尽管这些单个字母的类型通常是指泛型(完全是按照惯例):

class MyClass {
  T poorlyNamedTypeYuck() {
    // ...
  }
}

请注意,你不必将 T 作为返回类型:

&lt;T&gt; void thisIsAlsoValid(T genericUsedHere, List&lt;T&gt; orElseWhere) {
  // ...
}
英文:

In both cases, the return type is T.

If you see &lt;T&gt; before though, it means that the generic type T has been defined at the method level:

&lt;T extends JustAnExample&gt; T getThatThing() {
  // ...
}

If not, then it has probably been defined at the class level:

class MyClass&lt;T extends JustAnExample&gt; {
  T getThatThing() {
    // ...
  }
}

Or, it can technically also simply be a class named T, although those single-letter types usually refer to generics (purely by convention):

class MyClass {
  T poorlyNamedTypeYuck() {
    // ...
  }
}

Note that you don't have to use T as the return type:

&lt;T&gt; void thisIsAlsoValid(T genericUsedHere, List&lt;T&gt; orElseWhere) {
  // ...
}

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

发表评论

匿名网友

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

确定