简体   繁体   English

Java泛型分配全局变量

[英]Java generics to assign global variables

In this example, I get the warning that "List is a raw type." 在此示例中,我得到警告“列表是原始类型”。 How can I use generics to properly satisfy this warning? 如何使用泛型正确满足此警告? I'm having trouble because I can't figure out how to define "T" for the global variable like I was able to for the constructor signature. 我遇到了麻烦,因为我不知道如何像为构造函数签名那样为全局变量定义“ T”。

public class MyClass{

    private List input;

    public <T extends Comparable<T>> MyClass(List<T> input){
        this.input = input;
    }

}

You must define your class as parameterized 您必须将类定义为参数化

public class MyClass<T extends Comparable<T>>{

    private List<T> input;

    public MyClass(List<T> input){
        this.input = input;
    }

}

'Raw Types' refer to generic types that have not had their generic parameter types specific. “原始类型”是指没有特定泛型参数类型的泛型类型。 In this case, this is because you've omitted the generic type in your declaration of input . 在这种情况下,这是因为您在input的声明中省略了泛型类型。 Changing the declaration to: 将声明更改为:

private List<T> input;

Should resolve the error. 应该解决错误。 In addition, your constructor declaration doesn't need generic parameters, and can be shortened to: 此外,您的构造函数声明不需要通用参数,可以缩短为:

public MyClass(List<T> input){
    this.input = input;
}

You probably need to do something like: 您可能需要执行以下操作:

public class MyClass<T> {
    private List<T> input;
}

As you've seen, the issue is caused by your member variable input using List without a generic parameter. 如您所见,问题是由使用不带通用参数的List成员变量input引起的。 Hence, the type is being used "raw". 因此,该类型被“原始”使用。 To resolve this, you have three options. 要解决此问题,您有三个选择。 You can: 您可以:

  1. Make your whole class generic 使您的整个班级通用

     public class MyClass<T extends Comparable<T>> { private List<T> input; public MyClass(List<T> input) { this.input = input; } } 

or 要么

  1. Use a wildcard 使用通配符

     private List<? extends Comparable<T>> input; 

or 要么

  1. Rethink your strategy 重新考虑您的策略

      public class MyClass { private List<Comparable<T>> input; public <T extends Comparable<T>> MyClass(List<T> input) { this.input = new LinkedList<>(); // Obv. there are better ways of copying a list... for (Comparable<T> obj : input) { this.input.add(obj); } } } 

I would recommend option 1 or 3. 我建议选择1或3。

I believe your problem is where you say private List input; 我相信您的问题是您说private List input; , you game no parameter for the generic type that it's looking for. ,则无需为要查找的通用类型设置任何参数。 You can fix it by saying private List<T> input; 您可以通过说private List<T> input;来解决它private List<T> input;

You could make the class generic. 您可以使该类通用。

public class MyClass<T extends Comparable<T>> {

    private final List<T> input;

    public MyClass(Collection<? extends T> input){
        this.input = new ArrayList<T>(input);
    }
}

I have also changed the line this.input = input to instead make a copy of input . 我还更改了this.input = input以代替复制input This is really quite important because otherwise anyone with a reference to input could modify your class by modifying input . 这确实非常重要,因为否则任何引用input都可以通过修改input来修改您的类。 I've also made your constructor a bit more flexible by allowing any Collection of any subtype of T . 通过允许T的任何子类型的任何Collection ,我还使您的构造函数更加灵活。

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

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