String className ="test.Test1";
        Class clazz;
        try {
            clazz = Class.forName(className);
            Test1 userDao=(Test1) clazz.newInstance();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
这里className必须是完整的包名类名,另外这种方式通过Test1的这种方式进行实例化:
public Test1(){
        System.out.println("new Test1");
    }

 

相关文章:

  • 2022-12-23
  • 2021-06-14
  • 2021-10-04
  • 2021-12-21
  • 2022-12-23
  • 2022-01-24
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-30
  • 2021-10-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案