Java基础-局部内部类(3)

package com.hspedu.innerclass_;

public class Course414 {
    public static void main(String[] args) {
        // 局部内部类细节 2

        /*
         * 1、外部其他类不可以访问局部内部类(局部内部类是一个局部变量)
         * 2、局部内部类的成员和外部类的成员重名时,就近原则
         *  如果要使用外部类的成员,方式:外部类名.this.成员名
         * */

        Outer02 outer02 = new Outer02();
        outer02.m1();
        System.out.println(outer02.hashCode());
    }
}

class Outer02 {

    private int n1 = 100;

    private void m2() {
        System.out.println("Outer02 private m2");
    }

    public void m1() {
        // 局部内部类
        final class Inner02 {
            private int n1 = 800;

            public void f1() {
                System.out.println("this.n1/n1 = " + this.n1);   // 可以直接访问外部类的私有成员
                // Outer02.this本质指代的是外部类的对象
                System.out.println("outer n1 = " + Outer02.this.n1);
                System.out.println(Outer02.this.hashCode());
                m2();
            }
        }

        Inner02 inner02 = new Inner02();
        inner02.f1();

    }
}

 

posted @ 2022-03-04 16:33  柯南同学  阅读(20)  评论(0编辑  收藏  举报