@@ -24,6 +24,16 @@ public class MethodDemo {
2424 public static void main (String [] args ) throws InstantiationException , IllegalAccessException , NoSuchMethodException , SecurityException , IllegalArgumentException , InvocationTargetException , ClassNotFoundException {
2525 //Class<?> cls = Per.class;这个更下面这个是一样的
2626 Class <?> cls = Class .forName ("com.yale.test.java.fanshe.Per" );
27+ /*
28+ * Method getMethod(name, Class...):获取某个public的Method(包括父类)
29+ * Method getDeclaredMethod(name, Class...):获取当前类的某个Method(不包括父类)
30+ * Method[] getMethods():获取所有public的Method(包括父类)
31+ * Method[] getDeclaredMethods():获取当前类的所有Method(不包括父类)
32+ * getName():返回方法名称,例如:"getScore";
33+ * getReturnType():返回方法返回值类型,也是一个Class实例,例如:String.class;
34+ * getParameterTypes():返回方法的参数类型,是一个Class数组,例如:{String.class, int.class};
35+ * getModifiers():返回方法的修饰符,它是一个int,不同的bit表示不同的含义。
36+ */
2737 Method [] met = cls .getMethods ();
2838 for (int i =0 ;i <met .length ;i ++) {
2939 System .out .println ("这里会把Object父类的方法也打印出来:" + met [i ]);
@@ -43,6 +53,19 @@ public static void main(String[] args) throws InstantiationException, IllegalAcc
4353 Method getMethod = cls .getMethod ("get" + initcap (attribute ));
4454 Object ret = getMethod .invoke (obj );//相当于Person对象.getName()
4555 System .out .println ("通过反射调用对象的方法得到的返回值" + ret );
56+
57+ /*
58+ * 如果获取到的Method表示一个静态方法,调用静态方法时,由于无需指定实例对象,所以invoke方法传入的第一个参数永远为null。我们以Integer.parseInt(String)为例:
59+ */
60+ Method m = Integer .class .getMethod ("parseIne" , String .class );
61+ Integer res = (Integer )m .invoke (null , "99" );
62+ System .out .println (res );
63+
64+ /*
65+ * 和Field类似,对于非public方法,我们虽然可以通过Class.getDeclaredMethod()获取该方法实例,但直接对其调用将得到一个IllegalAccessException。
66+ * 为了调用非public方法,我们通过Method.setAccessible(true)允许其调用:
67+ * 使用反射调用方法时,仍然遵循多态原则:即总是调用实际类型的覆写方法(如果存在)。
68+ */
4669 }
4770 public static String initcap (String str ) {
4871 return str .substring (0 , 1 ).toUpperCase () + str .substring (1 );
0 commit comments