Reflection的getCallerClass使用
生活随笔
收集整理的這篇文章主要介紹了
Reflection的getCallerClass使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、通過使用Reflection的getCallerClass:可以得到調(diào)用者的類.
import sun.reflect.Reflection; public class T { public static void main(String[] args) { T2 t=new T2(); t.g(); } } class T2 { public void Ct(){ test(); } public void test(){ System.out.println(Reflection.getCallerClass(-1)); System.out.println(Reflection.getCallerClass(0)); System.out.println(Reflection.getCallerClass(1)); System.out.println(Reflection.getCallerClass(2)); System.out.println(Reflection.getCallerClass(3)); System.out.println(Reflection.getCallerClass(4)); System.out.println(Reflection.getCallerClass(5)); } }輸出:
class sun.reflect.Reflection //-1 class sun.reflect.Reflection //0 class com.Chloe.T2 //1 class com.Chloe.T2 //2 class com.Chloe.T //3 class com.intellij.rt.execution.application.AppMain //4 null //5由此可見:
調(diào)用getCallerClass(x) 傳參x的值當(dāng)
- 等于0 和小于0 - 返回 Reflection類
- 等于1 - 返回自己的類
- 等于2 - 返回調(diào)用者的類
- 等于3. 4. …n…層層上傳。
總結(jié)
以上是生活随笔為你收集整理的Reflection的getCallerClass使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 博弈论题目集 (持续更新)
- 下一篇: 抽象工厂模式的应用