[英]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.