额这个怎么说呢 给你举个小例子吧- try{
- Class tmpClass = Class.forName("Constructor1"); //class name, 如果有package,也需要添加上. i.e. com.abc.Constructor1;
- Class[] paramtersList = {String.class,Integer.TYPE}; // 构造method的参数(class)类型列表
- java.lang.reflect.Constructor constructor = tmpClass.getConstructor(paramtersList); //get constructor
- Object[] arg= new Object[] { "Test", Integer.valueOf(1)}; //set paramter 这里对应constructor
- Object object = constructor.newInstance(arg); //获得实例(<i>相当于</i> new Constructor1(“test”,1) )
- if(object != null) {
- System.out.println("success!");
- }
-
- }
- catch(Exception e) {
- e.printStackTrace();
- }
复制代码 |