簡體   English   中英

Test1和Test2在同一軟件包中,為什么我需要導入test2? 如果我不使用import,Inn inn1 = test2.new Inn(4),它將出錯

[英]Test1 & Test2 in the same package, so why do I need import test2? if I don't use import, Inn inn1 = test2.new Inn(4), It will go wrong

Test1和Test2在同一軟件包中,為什么我需要導入test2?

如果我不使用import,Inn inn1 = test2.new Inn(4),它將出錯。 &&其他問題:

public void show(final int number){

 }

如果我在這里使用final,這是什么意思?

 package info;
    import info.Test2.Inn;

    public class Test1 {
        public static void main(String[] args) {
            int sum = 1;
            Test2 test2 = new Test2(2);
            Test2.Inn inn = test2.new Inn(3);
            Inn inn1 = test2.new Inn(4);
            for (int i = 0; i < 9; i++){
                sum = (sum + 1) * 2;
            }
            System.out.println(sum);
        }
    }
    class Test2 {
        private int test;

        public Test2(int test) {
            this.test = test;
        }


        class Inn{
            private int inn;

            public Inn(int inn) {
                this.inn = inn;
            }

        }
    }

同一軟件包只能省略軟件包名稱。

整個簽名:

package_name.Test2.Inn
package_name.Test2

->

Test2.Inn
Test2

如果使用Inn代替Test2.Inn

如果在同一包中有一個名為Inn怎么辦?


final在方法簽名裝置number不能被修改。

因此,方法中不允許使用number++類的東西。

您可以按以下方式刪除導入語句

Test2.Inn inn1 = test2.new Inn(4);

因此,整個代碼可能看起來像

public class Test1 {
    public static void main(String[] args) {
        int sum = 1;
        Test2 test2 = new Test2(2);
        Test2.Inn inn = test2.new Inn(3);
        Test2.Inn inn1 = test2.new Inn(4);

        for (int i = 0; i < 9; i++){
            sum = (sum + 1) * 2;
        }
        System.out.println(sum);
    }


}

class Test2 {
    private int test;

    public Test2(int test) {
        this.test = test;
    }


    class Inn{
        private int inn;

        public Inn(int inn) {
            this.inn = inn;
        }

    }
}

我再給你看看

使Test2內部類成為Test1類

public class Test1 {
    public static void main(String[] args) {
        int sum = 1;
        Test2 test2 = new Test2(2);
        Test2.Inn inn = test2.new Inn(3);
        Test2.Inn inn1 = test2.new Inn(4);

        for (int i = 0; i < 9; i++){
            sum = (sum + 1) * 2;
        }
        System.out.println(sum);
    }

    class Test2 {
        private int test;

        public Test2(int test) {
            this.test = test;
        }


        class Inn{
            private int inn;

            public Inn(int inn) {
                this.inn = inn;
            }

        }
    }
}

但是代碼尚未編譯,因為您無法在Test1類的靜態方法中訪問Test2類。

所以,你可以這樣做

靜態類Test2

現在,您可以通過聲明Test2.Inn來訪問Test1的靜態成員Test2類及其內部類Inn。

public class Test1 {
    public static void main(String[] args) {
        int sum = 1;
        Test2 test2 = new Test2(2);
        Test2.Inn inn = test2.new Inn(3);
        Test2.Inn inn1 = test2.new Inn(4);

        for (int i = 0; i < 9; i++){
            sum = (sum + 1) * 2;
        }
        System.out.println(sum);
    }

    static class Test2 {
        private int test;

        public Test2(int test) {
            this.test = test;
        }


        class Inn{
            private int inn;

            public Inn(int inn) {
                this.inn = inn;
            }

        }
    }    
}

然后,上面的代碼有點難看,所以我終於可以編寫自己的代碼

import Test1.Test2.Inn;

public class Test1 {
    public static void main(String[] args) {
        int sum = 1;
        Test2 test2 = new Test2(2);
        Inn inn = new Inn(3);
        Inn inn1 = new Inn(4);

        for (int i = 0; i < 9; i++){
            sum = (sum + 1) * 2;
        }
        System.out.println(sum);
    }

    static class Test2 {
        private int test;

        public Test2(int test) {
            this.test = test;
        }


        static class Inn{
            private int inn;

            public Inn(int inn) {
                this.inn = inn;
            }

        }
    }    
}

我認為這全都與類的范圍或可訪問性有關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM