首页 > 其他 > 详细

当内部类和外部类存在同名成员时,如何访问外部类的成员

时间:2018-01-28 12:37:24      阅读:239      评论:0      收藏:0      [点我收藏+]
打印结果是什么 答案2
public class Test {

public static void main(String[] args) {
    Outer.Inner on = new Outer().new Inner();
    on.function();
}

}

class Outer {
int x = 0 ;
class Inner{
int x = 1;
void function(){
int x =2;
System.out.println("x="+x);
}
}
}
上述程序如何访问内部类的成员变量
public class Test {

public static void main(String[] args) {
    Outer.Inner on = new Outer().new Inner();
    on.function();
}

}

class Outer {
int x = 0 ;
class Inner{
int x = 1;
void function(){
int x =2;
System.out.println("x="+this.x);
}
}
}
如何访问外部类的成员变量
public class Test {

public static void main(String[] args) {
    Outer.Inner on = new Outer().new Inner();
    on.function();
}

}

class Outer {
int x = 0 ;
class Inner{
int x = 1;
void function(){
int x =2;
System.out.println("x="+Outer.this.x);
}
}
}

当内部类和外部类存在同名成员时,如何访问外部类的成员

原文:http://blog.51cto.com/13579086/2065943

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!