实例化参数化 class
instantiating parameterized class
只是想知道这样做是否正确。我想构造我的参数化 class 的实例,其中一个实例变量是泛型类型。下面的代码有效,但我在主要方法 "SomeObject is a raw type. References to generic type SomeObject should be parameterized".
中收到很多警告
public class SomeObject<T> {
private String description;
private T value;
public SomeObject(String description, T value) {
this.description = description;
this.value = value;
}
public static void main(String args[]){
List <SomeObject> objectList = new ArrayList<SomeObject>();
objectList.add(new SomeObject("Object 1: ", true));
objectList.add(new SomeObject("Object 2: ", 888.00));
objectList.add(new SomeObject("Object 3: ", "another object"));
objectList.add(new SomeObject("Object 4: ", '4'));
for (SomeObject object : objectList){
System.out.println(object.getDescription() + object.getValue());
}
}
}
The code below works but I get a lot of warnings in the main method
"Object is a raw type. References to generic type Object should be
parameterized".
警告是因为您在构造 SomeObject
时没有指定类型参数。即
应该是:
List<SomeObject<?>> objectList = new ArrayList<>();
objectList.add(new SomeObject<Boolean>("Object 1: ", true));
objectList.add(new SomeObject<Double>("Object 2: ", 888.00));
objectList.add(new SomeObject<String>("Object 3: ", "another object"));
objectList.add(new SomeObject<Character>("Object 4: ", '4'));
当你有一个没有类型参数(方括号中的部分)的 SomeObject
时,这称为 raw type,它与使用 [=11= 的擦除相同]. (基本上,擦除意味着它是非通用的。)
您还需要为 List
的 SomeObject
部分提供类型参数。这里我使用了通配符,这意味着列表可以包含任何类型的 SomeObject
,但是一旦我们将 SomeObject
放入列表中,我们就不知道它们的原始类型参数是什么了:
List<SomeObject<?>> objectList = new ArrayList<SomeObject<?>>();
objectList.add(new SomeObject<Boolean>("Object 1: ", true));
objectList.add(new SomeObject<Double>("Object 2: ", 888.00));
objectList.add(new SomeObject<String>("Object 3: ", "another object"));
objectList.add(new SomeObject<Character>("Object 4: ", '4'));
for (SomeObject<?> object : objectList) {
...;
}
只是想知道这样做是否正确。我想构造我的参数化 class 的实例,其中一个实例变量是泛型类型。下面的代码有效,但我在主要方法 "SomeObject is a raw type. References to generic type SomeObject should be parameterized".
中收到很多警告public class SomeObject<T> {
private String description;
private T value;
public SomeObject(String description, T value) {
this.description = description;
this.value = value;
}
public static void main(String args[]){
List <SomeObject> objectList = new ArrayList<SomeObject>();
objectList.add(new SomeObject("Object 1: ", true));
objectList.add(new SomeObject("Object 2: ", 888.00));
objectList.add(new SomeObject("Object 3: ", "another object"));
objectList.add(new SomeObject("Object 4: ", '4'));
for (SomeObject object : objectList){
System.out.println(object.getDescription() + object.getValue());
}
}
}
The code below works but I get a lot of warnings in the main method "Object is a raw type. References to generic type Object should be parameterized".
警告是因为您在构造 SomeObject
时没有指定类型参数。即
应该是:
List<SomeObject<?>> objectList = new ArrayList<>();
objectList.add(new SomeObject<Boolean>("Object 1: ", true));
objectList.add(new SomeObject<Double>("Object 2: ", 888.00));
objectList.add(new SomeObject<String>("Object 3: ", "another object"));
objectList.add(new SomeObject<Character>("Object 4: ", '4'));
当你有一个没有类型参数(方括号中的部分)的 SomeObject
时,这称为 raw type,它与使用 [=11= 的擦除相同]. (基本上,擦除意味着它是非通用的。)
您还需要为 List
的 SomeObject
部分提供类型参数。这里我使用了通配符,这意味着列表可以包含任何类型的 SomeObject
,但是一旦我们将 SomeObject
放入列表中,我们就不知道它们的原始类型参数是什么了:
List<SomeObject<?>> objectList = new ArrayList<SomeObject<?>>();
objectList.add(new SomeObject<Boolean>("Object 1: ", true));
objectList.add(new SomeObject<Double>("Object 2: ", 888.00));
objectList.add(new SomeObject<String>("Object 3: ", "another object"));
objectList.add(new SomeObject<Character>("Object 4: ", '4'));
for (SomeObject<?> object : objectList) {
...;
}