
java 中的泛型类型命名约定遵循一些通用准则,以使代码清晰一致。以下是一些标准约定:
t:类型(通常在有单个泛型参数时使用)。
public interface list<t> {
void add(t element);
t get(int index);
}
e:元素(通常用于集合)。
public interface collection<e> {
void add(e element);
e get(int index);
}
k、v:键和值(通常在地图中使用)。
public interface map<k, v> {
v put(k key, v value);
v get(k key);
}
n:数字(一般用来表示数字)。
public interface calculator<n extends number> {
n add(n a, n b);
n subtract(n a, n b);
}
s、u、v 等:多种类型(当有多个泛型参数时使用)。
public class Pair<S, T> {
private S first;
private T second;
public Pair(S first, T second) {
this.first = first;
this.second = second;
}
public S getFirst() {
return first;
}
public T getSecond() {
return second;
}
}
这些约定有助于使代码更具可读性并理解泛型参数的用途。









