如何解决警告:应该对通用类型的引用进行参数设置

问题描述 投票:0回答:2

我已经创建了通用接口。

public interface Abc<T> {
    void validatePojo(T input);
}

以下两类是上述接口的实现。

1)-----------------------------------------------
  public class Hello implements Abc<Pojo1> {
       @Override
       public void validatePojo(Pojo1 input) {
          // some code
       }
  }
2)-----------------------------------------------
  public class Hi implements Abc<Pojo2> {
       @Override
       public void validatePojo(Pojo2 input) {
          // some code
       }
   }

现在当我尝试创建Abc对象时,

T input = getInput(someInput);    // getInput return either Pojo1 or Pojo2
Abc abc = someFactory(someInput); //someFactory(someInput) will return either `new Hello()` 
  ^                               //or `new Hi()` based on `someInput`
  |
  +-------------------------------//warning
abc.validate(input);

public Abc<?> someFactory(final int input) {
    return input == 1 ? new Hi() : new Hello();
}

public T getInput(final int input) {
    return input == 1 ? new Pojo1() : new Pojo2();
}

我正在警告Abc is a raw type. References to generic type Abc<T> should be parameterized

我该如何爱护此警告?

我看了下面的互联网,但不是很有用。

  1. 我发现的一种方法是使用@SuppressWarnings
  2. [像Abc<Pojo1> abcAbc<Pojo2> abc那样声明变量,我不能这样做,因为使用Pojo1或Pojo2完全取决于输入。(我不想在这里编写工厂方法的逻辑)

还有其他方法可以爱它吗?

java generics interface warnings interface-implementation
2个回答
0
投票

编译器只是在通知您,Abc是通用的,并且取决于您在Factory中传递的类型。因此:


0
投票

Abc原始类型

© www.soinside.com 2019 - 2024. All rights reserved.