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/