class X{
private int a = 1;
private int b = 2;
class Y{
public void t2() {
int x = a;
int y = new X().b;
}
}
}
解语法糖后结果如下:
class X$Y {
/*synthetic*/ final X this$0;
X$Y(/*synthetic*/ final X this$0) {
this.this$0 = this$0;
super();
}
public void t2() {
int x = X.access$000(this$0);
int y = X.access$100(new X());
}
}
class X {
/*synthetic*/ static int access$100(X x0) {
return x0.b;
}
/*synthetic*/ static int access$000(X x0) {
return x0.a;
}
private int a = 1;
private int b = 2;
}
class Parent{
int a = 1;
}
class Test extends Parent{
public void md() {
int b = Test.super.a +super.a;
}
}
解语法糖后如下:
class Test extends Parent {
/*synthetic*/ static int access$001(Test x0) {
return x0.a;
}
public void md() {
int b = Test.access$001(this) + super.a;
}
}
注意super.a没有变化,而Test.super.a发生了变化
本文深入探讨了Java中内部类如何访问外部类的成员变量,以及super关键字在继承中的特殊用法。通过具体代码示例,展示了内部类访问外部类私有变量的语法糖机制,以及子类如何正确调用父类成员。对于理解Java类的封装性和继承性具有重要意义。
947

被折叠的 条评论
为什么被折叠?



