- class Outer{
- int x = 3;
- private class Inner{
- int x = 2;
- void function(){
- int x = 1;
- System.out.println("innner"+x);
- System.out.println("innner"+this.x);
- System.out.println("innner"+Outer.this.x);
- }
- }
- void method(){
- Inner in = new Inner();
- in.function();
- }
- }
- class InnerClassDemo{
- public static void main(String[] args) {
- Outer out = new Outer();
- out.method();
- }
- }
复制代码
如上代码,结果为1,2,3
原因 就近原则,当没有明确标出x所属对象获方法就就近当表明了所属对象或方法就是对应对象或方法的了 |