简体   繁体   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 in the same package, so why do I need import test2? Test1和Test2在同一软件包中,为什么我需要导入test2?

if I don't use import, Inn inn1 = test2.new Inn(4), It will go wrong. 如果我不使用import,Inn inn1 = test2.new Inn(4),它将出错。 && other question: &&其他问题:

public void show(final int number){

 }

If I use final here, What' this meaning? 如果我在这里使用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;
            }

        }
    }

The same package can only omit the package name. 同一软件包只能省略软件包名称。

The whole signature: 整个签名:

package_name.Test2.Inn
package_name.Test2

-> ->

Test2.Inn
Test2

If use Inn instead of Test2.Inn 如果使用Inn代替Test2.Inn

What if there is a class in same package with name Inn ? 如果在同一包中有一个名为Inn怎么办?


The final in method signature means number cannot be modified. final在方法签名装置number不能被修改。

So something like number++ is not allowed in the method. 因此,方法中不允许使用number++类的东西。

You can remove import statement as follows 您可以按以下方式删除导入语句

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

So, entire code might be looks, 因此,整个代码可能看起来像

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;
        }

    }
}

Let me show you another case 我再给你看看

Make Test2 inner class of the Test1 class 使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;
            }

        }
    }
}

But the code won't compile yet because you can not access Test2 class in the static method of Test1 class. 但是代码尚未编译,因为您无法在Test1类的静态方法中访问Test2类。

So, you can do this 所以,你可以这样做

static class Test2 静态类Test2

Now, you can access static member Test2 class of Test1 and its inner class Inn by declaring Test2.Inn 现在,您可以通过声明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;
            }

        }
    }    
}

Then, the above code is a little ugly so, I can finally make my own code 然后,上面的代码有点难看,所以我终于可以编写自己的代码

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;
            }

        }
    }    
}

I think it is all about scope or accessibility of class. 我认为这全都与类的范围或可访问性有关。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么test1()的运行速度比test2()快? - Why does test1() run much faster than test2()? 为什么`test2()`方法编译成功,但是`test1()`没有编译? - Why does `test2()` method compile successfully, but `test1()` doesn't? 仅在@ Test1通过时运行@ Test2 - Only run @Test2 when @Test1 passes 如何为Controller Inn Java编写模拟测试用例 - How to write mock test case for the controller inn java Test2阵列初始化错误 - Test2 Array Initialization Error 无法导入测试 package - Import of test package not possible 我有一个需要在所有测试中使用的测试方法,所以如何在硒的每个测试方法中使用它 - I have one test method that need to use in all the test, so how to use that in every test method in selenium 名称冲突:类型为test2的方法add(Object)具有与HashSet类型的add(E)相同的擦除 <E> 但不会覆盖它 - Name clash: The method add(Object) of type test2 has the same erasure as add(E) of type HashSet<E> but does not override it 当我什至不使用参数化测试时,为什么会出现 ParameterResolutionException? - Why do I have ParameterResolutionException when I don't even use parametrized test? 由以下原因引起:java.lang.IllegalStateException:顶点:test2 / document1的标签为“ StreamEntry”的多个输出边缘 - Caused by: java.lang.IllegalStateException: Multiple outgoing edges with label: “StreamEntry” for vertex: test2/document1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM