zoukankan      html  css  js  c++  java
  • eclipse中如何去除警告:Class is a raw type. References to generic type Class<T> should be parameterized

    转自: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());
    ...以下省略

  • 相关阅读:
    面向对象进阶
    初识面向对象
    模块和包
    day 17递归函数
    pip命令无法使用
    Python中的iteritems()和items()
    C# 截取字符串
    Python连接Mysql数据库
    【PYTHON】 Missing parentheses in call to 'print'
    javaScript中with函数用法实例分析
  • 原文地址:https://www.cnblogs.com/sharpest/p/6218863.html
Copyright © 2011-2022 走看看