- // public static void getMethodDemo() throws Exception {
- //
- // String className = "cn.itcast.domain.Person";
- // Class clazz = Class.forName(className);
- //
- // String methodName = "getName";
- //
- // Method method = clazz.getMethod(methodName, null);
- // Object obj = clazz.newInstance();
- // String value = (String)method.invoke(obj, null);
- // System.out.println(value);
- // }
-
- public static void getMethodDemo() throws Exception {
- person p = new Person();
- PropertyDescripteror pd = new PropertyDescriptor("name",person.class);
- Method m = pd.getReadMethod();
- String value = (String)m.invoke(p, null);
- System.out.println(value);
- }
- }
复制代码 这两段代码一个是用反射调用方法,一个是用内省的来调用的,看不出用内省有啥好处和方便的?
|
|