转自:https://blog.csdn.net/zwr_1022/article/details/78583872
解决前的源代码:
public class test {
public static void main(String args[]) {//入口
try {
//假设在同一个包中建的一个javaBean: person
Class c = Class.forName("person");//警告出现在这里
try {
person factory = (person) c.newInstance();
factory.setName("asdf");
System.out.println(factory.getName());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
------------------------------
- 解决方法1:增加编译注解@SuppressWarnings("unchecked")
public class test {
@SuppressWarnings("unchecked")//增加在这里
public static void main(String args[]) {
try {
Class c = Class.forName("person");//警告出现在这里
try {
person factory = (person) c.newInstance();
factory.setName("asdf");
...以下省略
------------------------------
- 解决方法2:使用泛型通配符
public class test {
public static void main(String args[]) {//入口
try {
Class<?> c = Class.forName("person");//这里使用泛型通配符
try {
person factory = (person) c.newInstance();
factory.setName("asdf");
System.out.println(factory.getName());
...以下省略