为什么是 S 和 T 而不是 A 和 B?
Why S and T instead of A and B?
这可能是一个关于泛型类型的愚蠢问题,但我找不到关于泛型类型遵循的命名约定的任何好的解释。
如果 class 有两个泛型; Java 程序员通常这样定义它:
class Foo<S extends Abc,T extends Pqr>{
S sVar;
T tVar;
}
为什么不是像这样简单的东西:
class Foo<A extends Abc,B extends Pqr>{
A aVar;
B bVar;
}
我只想问为什么要遵循这样的命名约定? S
和T
背后有什么原因吗?还是这些约定来自其他语言(比如 c++ 模板)?
Type Parameter Naming Conventions
By convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.
The most commonly used type parameter names are:
E - Element (used extensively by the Java Collections Framework)
K - Key
N - Number
T - Type
V - Value
S,U,V etc. - 2nd, 3rd, 4th types
You'll see these names used throughout the Java SE API and the rest of this lesson.
特别注意:"Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.",我当然同意。
大多数人会使用 T
和 S
,因为这是常见的做法,因此对于大多数其他人来说,可读。但我要补充一点,我看到名称 A
和 B
也用作 类 的示例名称:class A {...}
、class B {...}
。将它们用作类型变量名称会使它们难以区分。
引自 OCP.Oracle.Certified.Professional.Java.SE.8.Programmer.II.Study.Guide.Exam.1Z0809""
泛型的命名约定
类型参数可以任意命名。惯例是使用单
大写字母,以表明它们不是真正的 class 名字。以下是常用字母:
- E 表示 e元素
- K 一张地图 key
- V 为地图 value
- N 代表 n数字
- T 表示通用数据 type
- 多个通用类型的 S、U、V 等
这就是作者在约定背后的论点。我想使用 S、U 和 V 是因为它们在字母顺序方面更接近 T(代表类型)
这可能是一个关于泛型类型的愚蠢问题,但我找不到关于泛型类型遵循的命名约定的任何好的解释。 如果 class 有两个泛型; Java 程序员通常这样定义它:
class Foo<S extends Abc,T extends Pqr>{
S sVar;
T tVar;
}
为什么不是像这样简单的东西:
class Foo<A extends Abc,B extends Pqr>{
A aVar;
B bVar;
}
我只想问为什么要遵循这样的命名约定? S
和T
背后有什么原因吗?还是这些约定来自其他语言(比如 c++ 模板)?
Type Parameter Naming Conventions
By convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.
The most commonly used type parameter names are:
E - Element (used extensively by the Java Collections Framework) K - Key N - Number T - Type V - Value S,U,V etc. - 2nd, 3rd, 4th types
You'll see these names used throughout the Java SE API and the rest of this lesson.
特别注意:"Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.",我当然同意。
大多数人会使用 T
和 S
,因为这是常见的做法,因此对于大多数其他人来说,可读。但我要补充一点,我看到名称 A
和 B
也用作 类 的示例名称:class A {...}
、class B {...}
。将它们用作类型变量名称会使它们难以区分。
引自 OCP.Oracle.Certified.Professional.Java.SE.8.Programmer.II.Study.Guide.Exam.1Z0809""
泛型的命名约定
类型参数可以任意命名。惯例是使用单 大写字母,以表明它们不是真正的 class 名字。以下是常用字母:
- E 表示 e元素
- K 一张地图 key
- V 为地图 value
- N 代表 n数字
- T 表示通用数据 type
- 多个通用类型的 S、U、V 等
这就是作者在约定背后的论点。我想使用 S、U 和 V 是因为它们在字母顺序方面更接近 T(代表类型)