在Java中使用单字符泛型类型名称的原因是什么? [英] What is the reason for using single-character generic type names in java?

查看:65
本文介绍了在Java中使用单字符泛型类型名称的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,JDK中的大多数(或所有?)泛型类都具有个位数的泛型类型名称.例如,HashMap的定义如下:

In java most (or all?) generic classes in the JDK have single-digit generic type names. For example HashMap's definition looks like this:

public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable {

为什么这是约定,而不是像HashMap<KEY,VALUE>这样的更具描述性的类型名称?

Why is this the convention instead of more descriptive type names like HashMap<KEY,VALUE> ?

推荐答案

我认为这里的重点是简单约定.如果将单个字母用于泛型类型,将多个字母名称用于类名称,那么显然您要处理的是什么.另外,如果您直接使用KEY或VALUE,则这些名称将看起来像常量名称.惯例说:

I think the main point here is simple convention. If you use single-letter for generic types and multiple-letter names for class names it comes obvious what you are dealing with. Also, if you used KEY or VALUE as you point, these names would look as constant names. Convention says:

UPPERCASE_AND_UNDERSCORE -> CONSTANTS

UpperCamelCase -> ClassNames

lowerCamelCase -> attributes

T,S,V -> genericTypes

查看官方文档

类型参数命名约定

按照惯例,类型参数名称是单个大写字母.这与您已经知道的变量命名约定形成鲜明对比,并且有充分的理由:没有该约定,将很难分辨类型变量与普通类或接口名称之间的区别.

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-元素(由Java Collections Framework广泛使用)

E - Element (used extensively by the Java Collections Framework)

K-键

N-数字

T-类型

V-值

S,U,V等-第二,第三,第四类型

S,U,V etc. - 2nd, 3rd, 4th types

您将在Java SE API和本课程的其余部分中看到这些名称.

You'll see these names used throughout the Java SE API and the rest of this lesson.

这篇关于在Java中使用单字符泛型类型名称的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆