我之前总结的关于反射的另一篇文章:
http://www.cnblogs.com/DreamDrive/p/4217130.html
项目结构图:
CC类和SuperCC类是一种继承关系.想通过CC对象访问SuperCC中的方法.而建立一个DD客户端类.
//===============================分隔线一====================================================
CC.java
package c; public class CC extends SuperCC { }
SuperCC.java
package c; public class SuperCC { public void test() { System.out.println("This is SuperCC"); } }
DD.java
import java.lang.reflect.Method; import c.CC; import c.SuperCC; public class DD { public static void main(String[] args) { // CC对象访问SuperCC中的方法 // 方式一: CC cc = new CC(); cc.test(); // 方式二: try { // Method declaredTestMethod = cc.getClass().getDeclaredMethod("test"); 出现异常 java.lang.NoSuchMethodException:c.CC.test() Method declaredTestMethod = SuperCC.class.getDeclaredMethod("test"); // 正常打印出"This is SuperCC" declaredTestMethod.setAccessible(true); declaredTestMethod.invoke(cc); } catch (Exception e) { e.printStackTrace(); } } }
解释一下为什么在第一种情况下
cc.getClass().getDeclaredMethod("test");
会出现异常呢?
cc.getClaass 获取的值是 class c.CC
SuperCC.class 获取的值是 class c.SuperCC
如果在CC类中重写SuperCC中的test()方法.
C.java
package c; public class CC extends SuperCC { @Override public void test() { System.out.println("This is CC"); } }
DD.java
import java.lang.reflect.Method; import c.CC; import c.SuperCC; public class DD { public static void main(String[] args) { // CC对象访问SuperCC中的方法 // 方式一: CC cc = new CC(); cc.test(); // 方式二: try { //Method declaredTestMethod = cc.getClass().getDeclaredMethod("test"); // 正常打印出"This is CC" Method declaredTestMethod = SuperCC.class.getDeclaredMethod("test"); // 正常打印出"This is CC" declaredTestMethod.setAccessible(true); declaredTestMethod.invoke(cc); } catch (Exception e) { e.printStackTrace(); } } }
这两种方式都会正常打印出"This is CC".
//===============================分隔线二====================================================
再用一下toString()方法
C.java
package c; /** * created by kongxiaohan on Jul 21, 2015 Detailled comment * */ public class CC extends SuperCC { @Override public String toString() { System.out.println("This is CC toString"); return "CC [toString()=" + super.toString() + "]"; } }
SuperCC.java
package c; public class SuperCC { @Override public String toString() { System.out.println("This is SuperCC toString"); return "SuperCC [toString()=" + super.toString() + "]"; } }
DD.java
package d; import java.lang.reflect.Method; import c.CC; import c.SuperCC; public class DD { public static void main(String[] args) { CC cc = new CC(); try { // Method declaredTestMethod = Object.class.getDeclaredMethod("toString"); //正常 但是什么都不打印(也是正常的) // Method declaredTestMethod = cc.getClass().getDeclaredMethod("toString"); // 正常 分别打印 This is CC toString This is SuperCC toString Method declaredTestMethod = SuperCC.class.getDeclaredMethod("toString"); // 正常 分别打印 This is CC toString This is SuperCC toString // java.lang.NoSuchMethodException: c.SuperCC.toString() declaredTestMethod.setAccessible(true); declaredTestMethod.invoke(cc); } catch (Exception e) { e.printStackTrace(); } } }
同样如果以上CC.java中不重写toString()方法.
Method declaredTestMethod = cc.getClass().getDeclaredMethod("toString");是会报java.lang.NoSuchMethodException: c.CC.toString()
Method declaredTestMethod = SuperCC.class.getDeclaredMethod("toString");是正常打印的 This is SuperCC toString
//========================================================分割线三====================================================================