java中this$0 this$1 this$2


import java.lang.reflect.Field;

public class Outer {//this$0

public class FirstInner {//this$1

public class SecondInner {//this$2

public class ThirdInner {
}
}
}

public static void main(String[] args) throws Exception {

//初始化各内外实例
Outer test = new Outer();
FirstInner first = test.new FirstInner();
FirstInner.SecondInner second = first.new SecondInner();
FirstInner.SecondInner.ThirdInner third = second.new ThirdInner();

System.out.println(test.hashCode());
System.out.println(first.hashCode());
System.out.println(second.hashCode());

//Outer,this$0
Field outerfield = first.getClass().getDeclaredField("this$0");
outerfield.setAccessible(true);
Object object = outerfield.get(first);
System.out.println(object.getClass()+","+object.hashCode());

//FirstInner,this$1
Field firstInnerfied = second.getClass().getDeclaredField("this$1");
firstInnerfied.setAccessible(true);
object = firstInnerfied.get(second);
System.out.println(object.getClass()+","+object.hashCode());

//SecondInner,this$2
Field secondInnerfield = third.getClass().getDeclaredField("this$2");
secondInnerfield.setAccessible(true);
object = secondInnerfield.get(third);
System.out.println(object.getClass()+","+object.hashCode());

}
}

运行结果:

1163157884
1956725890
356573597
class my.test.Outer,1163157884
class my.test.Outer$FirstInner,1956725890
class my.test.Outer$FirstInner$SecondInner,356573597




免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM