1     /*
 2      * 通过反射创建带参数的对象
 3      */
 4     public Object Creatobject(String ClassPath, Object[] Params) throws Exception {
 5         Class<?> demo = null;
 6         Object obj = null;
 7         demo = Class.forName(ClassPath);
 8         if (Params != null) {
 9             Class[] argsClass = new Class[Params.length];
10             for (int i = 0, j = Params.length; i < j; i++) {
11                 argsClass[i] = Params[i].getClass();
12             }
13             Constructor cons = demo.getConstructor(argsClass);
14             obj = cons.newInstance(Params);
15         } else {
16             obj = demo.newInstance();
17         }
18         return obj;
19     }

说明:

参数 ClassPath 应为 包名+.+类名,类名不可带.java 后缀。

 

相关文章:

  • 2021-10-07
  • 2022-12-23
  • 2022-12-23
  • 2021-11-04
  • 2021-09-18
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-10
  • 2022-12-23
  • 2022-12-23
  • 2021-08-31
  • 2021-09-10
  • 2022-12-23
相关资源
相似解决方案