1. 程式人生 > 其它 >JAVA認識內部類

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