简体   繁体   English

实例化参数化类

[英]instantiating parameterized class

Just wondering if this is the right way to do it. 只是想知道这是否是正确的方法。 I want to construct instances of my parametrized class where one of the instance variables is a generic type. 我想构造我的参数化类的实例,其中实例变量之一是泛型类型。 The code below works but I get a lot of warnings in the main method "SomeObject is a raw type. References to generic type SomeObject should be parameterized". 下面的代码有效,但是在主要方法“ SomeObject是原始类型。应该对通用类型SomeObject的引用进行参数设置”时,我会收到很多警告。

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". 下面的代码可以工作,但是我在主要方法“对象是原始类型。应该对通用类型对象的引用进行参数化”方面收到很多警告。

the warning is because you haven't specified the type arguments when constructing the SomeObject . 警告是因为在构造SomeObject时没有指定类型参数。 ie. 即。

it should be: 它应该是:

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'));

When you have a SomeObject without a type argument (the part in the square brackets), that is called a raw type , and it's the same as using the erasure of SomeObject . 当您拥有不带类型SomeObject (方括号中的部分)时,这称为原始类型 ,这与使用擦除SomeObject (Basically, the erasure means it's non-generic.) (基本上,擦除表示它是非通用的。)

You also need to provide a type argument to the SomeObject part of the List . 您还需要为ListSomeObject部分提供类型参数。 Here I've used a wildcard, which means the list can hold any type of SomeObject , but once we put a SomeObject in to the list we don't know what their original type argument was anymore: 在这里,我使用了通配符,这意味着列表可以容纳任何类型的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) {
    ...;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM