我正在尝试运行在同一个包中调用保护类的文件 Demo.java,但它给出了错误这是主类。
package p1;
// Instantiate the various classes in p1.
class Demo {
public static void main(String args[]) {
Protection ob1 = new Protection();
//Derived ob2 = new Derived();
//SamePackage ob3 = new SamePackage();
}
}
这是我想在主类中使用的类。
package p1;
public class Protection {
public 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);
}
}
它给出了这个错误:
$ javac Demo.java
Demo.java:6: error: cannot find symbol
Protection ob1 = new Protection();
^
symbol: class Protection
location: class Demo
Demo.java:6: error: cannot find symbol
Protection ob1 = new Protection();
^
symbol: class Protection
location: class Demo
2 errors
error: compilation failed