简体   繁体   English

理解Java泛型。输入参数约定

[英]Understanding Java generics. Type parameter conventions

The most commonly used type parameter names are: 最常用的类型参数名称是:

E - Element (used extensively by the Java Collections Framework) E - Element(Java Collections Framework广泛使用)

K - Key K - 钥匙

N - Number N - 数字

T - Type T型

V - Value V - 价值

S,U,V etc. - 2nd, 3rd, 4th types S,U,V等 - 第2,第3,第4类型

I don't seem to quite understand what exactly does every letter correspond to. 我似乎不太明白每个字母到底对应的是什么。 I understand that each letter represents just a convention but what does 2nd, 3rd and 4th type mean exactly? 我知道每个字母只代表一个惯例,但第二,第三和第四类是什么意思呢? When should I use what? 我什么时候应该用什么? On their official tutorials website it doesn't give further information. 在他们的官方教程网站上,它没有提供进一步的信息。

Some examples: 一些例子:

  • Map<K, V> : A map usually assigns V alues to K eys. Map<K, V> :地图通常将V值分配给K eys。 These are special kinds of types, so they are used here. 这些是特殊类型,因此在这里使用。
  • List<E> : A list contains E lements. List<E> :列表包含E It's a convention that they are called elements. 这是一种惯例,它们被称为元素。 On the other hand, T would also be acceptable here. 另一方面, T在这里也是可以接受的。
  • Formatter<T> : A formatter can format any T ype. Formatter<T> :格式化程序可以格式化任何T形式。 It's not really an element, nor a key, nor a value, so T is the correct letter here. 它不是一个元素,也不是一个键,也不是一个值,所以T就是这里的正确字母。
  • Triplet<T, U, V> : A triplet for arbitrary types. Triplet<T, U, V> :任意类型的三元组。 Since the type definition does not know anything about the types that will be filled in later, it uses just the T for the first type, followed by the next letters in alphabetical order. 由于类型定义对稍后将填充的类型一无所知,因此它仅使用T作为第一种类型,然后按字母顺序使用下一个字母。

I strongly recommend longer names, especially when you have more than one type parameter. 我强烈建议使用更长的名称,尤其是当您有多个类型参数时。

public class MyMap<Key, Value> {...}

If you insist on a convention, you may want to add "Type" to the end of the name, like 如果你坚持一个约定,你可能想在名称的末尾添加“类型”,比如

public class MyMap<KeyType, ValueType> {...}

(Though I prefer to just have the name without a suffix. The name should be UpperCamelCase ) (虽然我更喜欢没有后缀的名称。名称应该是UpperCamelCase

As you know, the letters don't mean anything in themselves and are just conventions to make reading the code a bit easier. 如您所知,这些字母本身并不代表任何内容,只是使阅读代码更容易的惯例。 The "2nd, 3rd and 4th types" bit just means when you have a parameterised type with multiple arguments you should call those arguments S, T, U, V etc. For example “第2,第3和第4类型”位只是意味着当你有一个带有多个参数的参数化类型时,你应该调用那些参数S,T,U,V等。例如

class MyClass<S, T, U> {
}

is a class with three type parameters. 是一个有三个类型参数的类。

Obviously you don't have to use S, T and U. If a more meaningful convention can be used then you should do so, for example 很明显,你不必使用S,T和U.如果然后可以使用更有意义的会议,你应该这样做,例如

class Car<W, E, P> {
}

could be a Car class parameterised on wheel, engine and paint type. 可以是车轮,发动机和油漆类型参数化的Car类。

EDIT: As the other answer pointed out, even better would be: 编辑:正如另一个答案指出的那样,更好的是:

class Car<WheelType, EngineType, PaintType> {
}

I don't seem to quite understand what exactly does every letter correspond to. 我似乎不太明白每个字母到底对应的是什么。 I understand that each letter represents just a convention but what does 2nd, 3rd and 4th type mean exactly? 我知道每个字母只代表一个惯例,但第二,第三和第四类是什么意思呢?

The reason that you don't "quite understand what exactly does every letter correspond to" is that the use of single letter type parameter names is an informal convention. 你不“完全理解每个字母对应的内容”的原因是单字母类型参数名称的使用是一种非正式的约定。 The letters don't and cannot have fixed meanings. 这些字母没有,也没有固定的含义。 You are expected to read the javadocs (and if necessary, the code) to figure out what the letters mean in the context of the classes . 您应该阅读javadoc(以及必要时的代码)来确定字母在类上下文中的含义。 In most cases, there is some kind of meaning intended by the code author. 在大多数情况下,代码作者有某种意义。 Just figure it out. 试一试。

When should I use what? 我什么时候应该用什么?

If you are following a coding standard that says something on this subject, then do what it says. 如果您遵循的是关于此主题的说明的编码标准,那么就按照它说的做。 Otherwise: 除此以外:

  • try to be consistent with the emerging conventions of your existing codebase as you see them , and 尝试与您现有的代码库中的新兴约定保持一致, 如您所见
  • use your common sense, and do what gives the most readable code. 使用你的常识,做一些提供最可读代码的东西。

On their official tutorials website it doesn't give further information. 在他们的官方教程网站上,它没有提供进一步的信息。

That is correct. 那是正确的。

The "official" Java style guide document has not been updated for ~10 years. “官方”Java风格指南文档尚未更新约10年。 On the one hand, it is a shame that Sun / Oracle no longer see formalizing style conventions as their role. 一方面,令人遗憾的是,Sun / Oracle不再将风格约定正式化为其角色。 On the other hand, I can understand why they don't. 另一方面,我可以理解为什么他们不这样做。 (There is no "value" to Sun, and the potential aggravation for the people involved is simply not worth it. If you've been in one of those pointless debates about the "right" place to put curly brackets, spaces and so on, and on, and on, you'll know what I mean.) (对Sun来说没有“价值”,所涉人员的潜在恶化根本不值得。如果你曾经参与其中一个关于“正确”放置大括号,空格等的“无权”的辩论。 ,等等,你知道我的意思。)

I typically just represent the generic type as if it were a class name: 我通常只表示泛型类型,就好像它是一个类名:

abstract class DAO<Entity, Id> {
   abstract Entity findById(Id id);
}

IMO, your code should be semantic almost to the point where it's like reading sentences instead of code. IMO,你的代码应该是语义几乎到了读取句子而不是代码的程度。 To me, this isn't very readable or semantic: 对我来说,这不是很可读或语义:

abstract class DAO<E, I> {
   abstract E findById(I id);
}

If all else just falls short, just call it what it is. 如果其他一切都不尽如人意,那么就把它称之为它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM