import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class Client { public Client(String s,Integer i){ System.out.println(s); System.out.println(i); } public static void main(String[] args) { test(); } public static void test(){ String className = ""; className = "com.sky.hrms.demo.Client"; try { Class clazz = Class.forName(className); //构造构造方法参数类型 Class[] parameters = new Class[]{String.class, Integer.class}; //获得指定参数类型的构造方法 Constructor constructor = clazz.getConstructor(parameters); //构造具体参数数组 Object[] objects = new Object[]{"String", 2}; //用指定参数的构造方法创建对象,具体应用是一般来说不会是Client,而是Client实现的接口或继承的父类 Client c = (Client) constructor.newInstance(objects); //调用Client中的方法 c.print();//String,2,Client } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } public void print(){ System.out.println("Client"); } }