gpt4 book ai didi

java - Test1和Test2在同一个包中,那么为什么我需要导入test2呢?如果我不使用import,Inn inn1 = test2.new Inn(4),就会出错

转载 作者:太空宇宙 更新时间:2023-11-04 11:14:26 24 4
gpt4 key购买 nike

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++ 之类的内容。

关于java - Test1和Test2在同一个包中,那么为什么我需要导入test2呢?如果我不使用import,Inn inn1 = test2.new Inn(4),就会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45646241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com