我已经创建了通用接口。
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
。
我该如何爱护此警告?
我看了下面的互联网,但不是很有用。
@SuppressWarnings
。Abc<Pojo1> abc
或Abc<Pojo2> abc
那样声明变量,我不能这样做,因为使用Pojo1或Pojo2完全取决于输入。(我不想在这里编写工厂方法的逻辑)还有其他方法可以爱它吗?
编译器只是在通知您,Abc是通用的,并且取决于您在Factory中传递的类型。因此:
Abc
是原始类型