package p1;
public class Protection {
int n = 1;
private int n_pri = 2;
protected int n_pro = 3;
public int n_pub = 4;
public Protection() {
System.out.println("base constructor");
System.out.println("n = " + n);
System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
包p2包含三个文件demo.java,OtherPackage.java和Protection2.java:
//Demo package p2.
package p2;
//Instantiate various classes in p2.
public class Demo {
public static void main(String[] args) {
Protection2 ob1 = new Protection2();
OtherPackage ob2 = new OtherPackage();
}
}
package p2;
OtherPackage.java
class OtherPackage {
OtherPackage() {
p1.Protection p = new p1.Protection();
System.out.println("Other package constructor");
//class or package only
//System.out.println("n = " + p.n);
//class only
//System.out.println("n_pri = " + p.n_pri);
//class, subclass or package only
//System.out.println("n_pro = " + p.n_pro)
System.out.println("n_pub = " + p.n_pub);
}
}
和Protection2.java
package p2;
class Protection2 extends p1.Protection {
Protection2() {
System.out.println("Derived other package constructor");
//class or package only
//System.out.println("n = " + n);
//class only
//System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
我尝试从包p2编译Demo.java:
:~/Desktop/Examples/p2$ javac *.java
我生成以下错误
Protection2.java:3: error: package p1 does not exist
class Protection2 extends p1.Protection {
^
OtherPackage.java:5: error: package p1 does not exist
p1.Protection p = new p1.Protection();
^
OtherPackage.java:5: error: package p1 does not exist
p1.Protection p = new p1.Protection();
^
Protection2.java:13: error: cannot find symbol
System.out.println("n_pro = " + n_pro);
^
symbol: variable n_pro
location: class Protection2
Protection2.java:14: error: cannot find symbol
System.out.println("n_pub = " + n_pub);
^
symbol: variable n_pub
location: class Protection2
5 errors
我尝试导入包p1,但它增加了另外一个错误以及上述5个错误:
Demo.java:4: error: package p1 does not exist
import p1.Protection;
答案 0 :(得分:0)
我制作了这个目录结构:
package
|
----------------------------
| |
source bin
| |
( All .java files ) ( Package folders after compilation )
Java文件:
Protection.java
package p1;
public class Protection {
public int m_Pub;
int m_N;
private int m_Pri;
protected int m_Pro;
public Protection () {
m_Pub = 4;
m_N = 1;
m_Pri = 2;
m_Pro = 3;
System.out.println ( "Base Constructor" );
System.out.println ( "m_Pub = " + m_Pub );
System.out.println ( "m_N = " + m_N );
System.out.println ( "m_Pri = " + m_Pri );
System.out.println ( "m_Pro = " + m_Pro );
}
}
Demo.java
package p2;
public class Demo {
public static void main ( String [] args ) {
Protection2 protection2 = new Protection2 ();
OtherPackage otherPackage = new OtherPackage ();
}
}
OtherPackage.java
package p2;
import p1.Protection;
public class OtherPackage {
public OtherPackage () {
Protection protection = new Protection ();
System.out.println ( "OtherPackage Constructor" );
}
}
Protection2.java
package p2;
import p1.Protection;
public class Protection2 extends Protection {
public Protection2 () {
System.out.println ( "Derived Protection2 Constructor" );
System.out.println ( "m_Pro = " + m_Pro );
System.out.println ( "m_Pub = " + m_Pub );
}
}
<强>汇编:强>
nipal_000@Lenovo-PC /cygdrive/c/mine/java/package
$ javac -d bin source/*.java
<强>执行:强>
nipal_000@Lenovo-PC /cygdrive/c/mine/java/package
$ cd bin
nipal_000@Lenovo-PC /cygdrive/c/mine/java/package/bin
$ java p2.Demo
Base Constructor
m_Pub = 4
m_N = 1
m_Pri = 2
m_Pro = 3
Derived Protection2 Constructor
m_Pro = 3
m_Pub = 4
Base Constructor
m_Pub = 4
m_N = 1
m_Pri = 2
m_Pro = 3
OtherPackage Constructor
nipal_000@Lenovo-PC /cygdrive/c/mine/java/package/bin
重点是,在编译时您未能指定,在哪里查找package p1
,因为您位于文件夹p2
内。在编译过程中,您需要放置所有.java
个文件。
如果您尝试将每个.java
文件保存在相应的package
文件夹中,那么它会是这样的:
目录结构:
package
|
----------------------------
| |
source bin
| |
------------ ( Package folders after compilation )
| |
p1 p2
| |
| Demo.java OtherPackage.java Protection2.java
Protection.java
<强>汇编:强>
nipal_000@Lenovo-PC /cygdrive/c/mine/java/package
$ javac -d bin source/p1/*.java source/p2/*.java
<强>执行:强>
nipal_000@Lenovo-PC /cygdrive/c/mine/java/package
$ cd bin
nipal_000@Lenovo-PC /cygdrive/c/mine/java/package/bin
$ java p2.Demo
Base Constructor
m_Pub = 4
m_N = 1
m_Pri = 2
m_Pro = 3
Derived Protection2 Constructor
m_Pro = 3
m_Pub = 4
Base Constructor
m_Pub = 4
m_N = 1
m_Pri = 2
m_Pro = 3
OtherPackage Constructor
nipal_000@Lenovo-PC /cygdrive/c/mine/java/package/bin