此链接指出以下内容:
具有实际类型参数的泛型类型的实例化称为参数化类型.示例(参数化类型):
Collection<String> coll = new LinkedList<String>();
那么什么是参数化类型?
Collection<String> 要么LinkedList<String>Ale*_*exR 11
参数化类型通常是一个处理其他对象的类,而不管它是什么类型.可以使用符号"name"定义类型,然后在创建类的实例时传递.
例如:
class MyClass<T> {
private T obj;
public MyClass<T>(T obj) {
this.obj = obj;
}
public int getId() {
return obj.hashCode();
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,MyClass包装任何类型的对象并hashCode()使用此方法始终存在的事实来执行其方法.
以下是此类的使用方法:
int sid = new MyClass<String>("aaaa").hashCode();
请注意,您不能说 new MyClass<String>(123):使用参数创建对象的事实String决定了构造函数参数的类型.
回到你的例子Collection<String>意味着"字符串的集合".这意味着您无法将其他类型的对象添加到此集合中.
似乎泛型类型和参数化类型是同义词.但是,Box<Integer>从通用类型的意义上来说,似乎不再称之为通用类型.可能:
泛型类型是具有正式类型参数的声明
class Box<T> { .. }
Run Code Online (Sandbox Code Playgroud)
Box<T> 是一种通用类型
参数化类型是具有实际类型参数/ s的声明
Box<Integer> b;
Run Code Online (Sandbox Code Playgroud)
Box<Integer> 是参数化类型
原始类型是泛型类型的声明,没有实际类型参数/ s.
class Box<T> { .. }
Box b;
Run Code Online (Sandbox Code Playgroud)
Box 是一种原始类型
| 归档时间: |
|
| 查看次数: |
30138 次 |
| 最近记录: |