Java Reflection类

Reflection类

getCallerClass()。返回调用者Class对象。
例如:
 1 public class Test1 {
 2     public void mthod1() {
 3         Class clazz = Reflection.getCallerClass();
 4         System.out.println("test1调用方法:"+clazz.getSimpleName());
 5         new Test2().test2();
 6     }
 7 }
 8 
 9 class Test2{
10     public void test2() {
11         Class clazz = Reflection.getCallerClass();
12         System.out.println("test2调用方法:"+clazz.getSimpleName());
13     }
14     public static void main(String[] args) {
15         new Test1().mthod1();
16     }
17 }
18 输出:
19 test1调用方法:Test2
20 test2调用方法:Test1
View Code

猜你喜欢

转载自www.cnblogs.com/hf-china/p/9195263.html