JAVA基础---认识内部类

Posted 一位懒得写博客的小学生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA基础---认识内部类相关的知识,希望对你有一定的参考价值。

实例内部类

实例化内部类对象

public class OutClass {
    public int val1;
    public int val;
    class InnerClass {
        public int val3;
        public int val4;
    }

    public static void main(String[] args) {
        OutClass out = new OutClass();
        OutClass.InnerClass inner = out.new InnerClass();
    }
}
public class OutClass {
    public int val1 = 1;
    public int val2 = 2;
    public void fun() {
        System.out.println("OutClass");
        System.out.println(val1);
        System.out.println(val2);
    }
    class InnerClass {
        public int val3 = 3;
        public int val4 = 4;
        public void func(){
            System.out.println("InnerClass");
            System.out.println(val3);
            System.out.println(val4);
            //内部类可以调用外部类的成员属性和成员方法
            System.out.println(val1);
            System.out.println(val2);
            System.out.println("-----------------");
            fun();
        }
    }

    public static void main(String[] args) {
        OutClass out = new OutClass();
        OutClass.InnerClass inner = out.new InnerClass();
        inner.func();
    }
}

//执行结果
InnerClass
3
4
1
2
-----------------
OutClass
1
2

在实例内部类中不能定义静态成员属性
在这里插入图片描述

如果非要定义,则
在这里插入图片描述

public class OutClass {
    public int val1 = 1;
    public int val2 = 2;
    class InnerClass {
        public int val1 = 11;
        public int val3 = 3;
        public int val4 = 4;
        public void func(){
            System.out.println("InnerClass");
            System.out.println(val1);
            System.out.println(this.val1);
            System.out.println(OutClass.this.val1);
        }
    }

    public static void main(String[] args) {
        OutClass out = new OutClass();
        OutClass.InnerClass inner = out.new InnerClass();
        inner.func();
    }
}
//实例内部类实际上拥有2个this,一个是实力内部类自己的,一个是外部类的

//执行结果
InnerClass
11
11
1

静态内部类

public class OutClass {
    public static int val1 = 1;
    public int val2 = 2;
    static class InnerClass {
        public int val3 = 3;
        public int val4 = 4;
        public void func(){
            System.out.println("InnerClass");
            System.out.println(val3);
            System.out.println(val4);
            System.out.println("---------");
            System.out.println(val1); //此时调用外部类的属性需要加static
            //System.out.println(val2);  //在静态内部类当中,不可以访问外部类的非静态成员,外部类的非静态成员是依赖于外部对象的。
        }
    }

    public static void main(String[] args) {
        OutClass.InnerClass inner = new OutClass.InnerClass();
        inner.func();
    }
}

//执行结果
InnerClass
3
4
---------
1

//如果非要访问外部类非静态成员
public class OutClass {
    public static int val1 = 1;
    public int val2 = 2;
    static class InnerClass {
        public int val3 = 3;
        public int val4 = 4;
        public OutClass out;
        public InnerClass(OutClass out) {
            this.out = out;
        }

        public void func(){
            System.out.println("InnerClass");
            System.out.println(val3);
            System.out.println(val4);
            System.out.println("---------");
            System.out.println(val1);
            System.out.println(out.val2);

        }
    }

    public static void main(String[] args) {
        OutClass out = new OutClass();
        OutClass.InnerClass inner = new OutClass.InnerClass(out);
        inner.func();
    }
}

//执行结果
InnerClass
3
4
---------
1
2

匿名内部类

public class OutClass {
    public static int val1 = 1;
    public int val2 = 2;
    public void func() {
        System.out.println("hello");
    }

    public static void main(String[] args) {
        new OutClass(){
            @Override //重写外部类func方法
            public void func() {
                super.func();
                System.out.println(val1);
                System.out.println(val2);
            }
        }.func();
    }
}

//执行结果
hello
1
2

以上是关于JAVA基础---认识内部类的主要内容,如果未能解决你的问题,请参考以下文章

JAVA基础---认识内部类

JAVA-初步认识-第十章-匿名内部类-概述

重新认识java ---- 内部类

JAVA-初步认识-第十章-匿名内部类-应用

JAVA-初步认识-第十章-内部类-细节

JAVA-初步认识-第十章-内部类-局部内部类