从另一个包访问 class
Access class from another package
我在名为 asdsad 的文件夹中有两个文件夹
.
├── a
│ └── A.java
├── b
│ └── B.java
这是我的A.java
package asdsad.a;
public class A {
public A() {
}
public int number;
}
这是我的B.java
package asdsad.b;
import asdsad.a.A;
public class B {
public B() {
}
public int n = 5;
public static void main(String[] args) {
A a = new A();
System.out.println(a.number);
}
}
现在我在 asdsad 文件夹内的终端中。我执行javac a/A.java
,我没有任何错误。但是当我尝试 javac b/B.java
时,我得到了这些错误:
b/B.java:2: error: cannot find symbol
import asdsad.a.A;
^
symbol: class A
location: package asdsad
b/B.java:12: error: cannot find symbol
A a = new A();
^
symbol: class A
location: class B
b/B.java:12: error: cannot find symbol
A a = new A();
^
symbol: class A
location: class B
3 errors
Now I'm in my terminal inside asdsad folder
已经错了。您应该在包含它的文件夹中。一顶。
I execute javac a/A.java, I don't have any errors.
但你还是错了。你应该执行 javac asdsad/a/A.java
.
But when I try to javac b/B.java
又错了。应该是 javac asdsad/b/B.java
.
我在名为 asdsad 的文件夹中有两个文件夹
.
├── a
│ └── A.java
├── b
│ └── B.java
这是我的A.java
package asdsad.a;
public class A {
public A() {
}
public int number;
}
这是我的B.java
package asdsad.b;
import asdsad.a.A;
public class B {
public B() {
}
public int n = 5;
public static void main(String[] args) {
A a = new A();
System.out.println(a.number);
}
}
现在我在 asdsad 文件夹内的终端中。我执行javac a/A.java
,我没有任何错误。但是当我尝试 javac b/B.java
时,我得到了这些错误:
b/B.java:2: error: cannot find symbol
import asdsad.a.A;
^
symbol: class A
location: package asdsad
b/B.java:12: error: cannot find symbol
A a = new A();
^
symbol: class A
location: class B
b/B.java:12: error: cannot find symbol
A a = new A();
^
symbol: class A
location: class B
3 errors
Now I'm in my terminal inside asdsad folder
已经错了。您应该在包含它的文件夹中。一顶。
I execute javac a/A.java, I don't have any errors.
但你还是错了。你应该执行 javac asdsad/a/A.java
.
But when I try to javac b/B.java
又错了。应该是 javac asdsad/b/B.java
.