java - 什么是 Java 中的未经检查的调用?
问题描述
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) 给了我一个警告“未经检查地调用 'set(T)' 作为原始类型 'Main' 的成员”。什么是未经检查的调用,为什么我会得到它,即使 set 方法有效并且在执行 print 语句后,会打印 3。
解决方案
Main
您还没有告诉编译器该test
变量指的是哪种类型。就编译器而言,它可能是Main<Integer>
orMain<String>
或Main<RahatsCrazyClass>
。所以编译器不能保证这test.set(3)
是有道理的——你可能试图Integer
在Main<String>
.
警告告诉您编译器遇到了无法保证感觉的情况。你应该避免有这种事情。test
声明为 aMain<Integer>
比声明为 a更好Main
。
推荐阅读
- javascript - 如何在网站上制作 discord.js 机器人统计信息?
- reactjs - 如何在反应传单中更改具体国家的颜色/边框颜色
- django - Django 的缓存框架是每个用户还是所有用户的
- java - 使用 PDFBOX 类时 Java 8 ThreadPoolExecutor 不执行
- javascript - 我可以在 React/Preact 中为 useEffect 挂钩使用动态选择的函数吗?
- azure - Terraform - Azure 上的静态 IP 地址
- keras - 在一定数量的 epoch 之后,Colab GPU 执行变得非常缓慢
- swift - 在 SwiftUI 中删除/替换/添加菜单
- flutter - Flutter 检查屏幕在 Android 上是圆形的
- c# - 实体框架 OrderBy 日期列表