public class Main<T> {
T obj;
public Main(T input) {
this.obj = input;
}
void set(T input) {
this.obj = input;
}
void print() {
System.out.println(this.obj);
}
public static void main(String[] args) {
Main<Integer> tester = new Main<>(2);
Main test = tester;
test.print();
test.set(3);
test.print();
}
}
在上面的代码中 test.set(3) 给了我一个警告“作为原始类型 'Main' 的成员对 'set(T)' 的未经检查的调用”。 什么是未经检查的调用以及为什么会得到它,即使 set 方法有效并且执行 print 语句后,仍打印 3。
最佳答案
你还没有告诉编译器什么样的Main
test
变量指的是.就编译器而言,它可能是 Main<Integer>
或Main<String>
或Main<RahatsCrazyClass>
。所以编译器不能保证test.set(3)
有道理 - 您可能会尝试设置 Integer
Main<String>
内的对象。
该警告告诉您编译器遇到了无法保证其意义的情况。你应该避免发生这种事情。最好申报test
作为Main<Integer>
不仅仅是 Main
.
关于java - Java 中的未检查调用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60272016/