try {
//利用反射拿到我们要的class类
Class clazz = Class.forName("com.luch.sample.common.thread.Jack");
//拿到类的属性名
Field field = clazz.getField("name");
//获取属性的值
String obj = (String) field.get(clazz.newInstance());
System.out.println(obj);
Field[] fields = clazz.getDeclaredFields();
for(Field fld: fields) {
obj = (String) fld.get(clazz.newInstance());
System.out.println(obj);
}
//拿到类的方法,null是方法的参数
Method method = clazz.getMethod("test", null);
method.invoke(clazz.newInstance(), null);
//拿到类的方法,null是方法的参数
method = clazz.getMethod("fetchAge", new Class[]{String.class});
method.invoke(clazz.newInstance(), new Object[]{new String("1987-07-15")});
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}