无法在Java中继承同一个包

时间:2014-03-14 15:30:02

标签: java

我还在学习Java,我的问题是我无法继承同一个包。 *请事先详细解释

包装中的第1类:

package Jpak02;

public class PTaccess_modes {
    public int a = 1;
    protected int b = 2;
    int c = 3;
    private int d = 4;

    PTaccess_modes() {
        System.out.println("\nInside PTaccess_mode class in package Jpak02");
        System.out.println("\nPublic int a : " + a);
        System.out.println("\nProtected int b : " + b);
        System.out.println("\nDefault int c : " + c);
        System.out.println("\nPrivate int d : " + d);
    }
}

同一包装中的第2类:

package Jpak02;

class PTaccess_modes_1 extends PTaccess_modes {
    PTaccess_modes_1() {
        System.out.println("\nInside PTaccess_mode_1 class in package Jpak02");
        System.out.println("\nPublic int a : " + a);
        System.out.println("\nProtected int b : " + b);
        System.out.println("\nDefault int c : " + c);
        System.out.println("\nPrivate int d : " + d);
    }
}

编译错误:

      D:\softwareinstall\Java_progs\packages\Jpak02>javac PTaccess_modes_1.java
      PTaccess_modes_1.java:5: error: cannot find symbol
      class PTaccess_modes_1 extends PTaccess_modes
                                ^
        symbol: class PTaccess_modes
      PTaccess_modes_1.java:10: error: cannot find symbol
        System.out.println("\nPublic int a : "+a);
                                          ^
        symbol:   variable a
        location: class PTaccess_modes_1
      PTaccess_modes_1.java:11: error: cannot find symbol
        System.out.println("\nProtected int b : "+b);
                                             ^
        symbol:   variable b
        location: class PTaccess_modes_1
      PTaccess_modes_1.java:12: error: cannot find symbol
        System.out.println("\nDefault int c : "+c);
                                           ^
        symbol:   variable c
        location: class PTaccess_modes_1
      4 errors

2 个答案:

答案 0 :(得分:1)

我刚刚创建了一个名为Jpak02的新文件夹,然后在里面创建了两个文件:PTaccess_modes.java和PTaccess_modes_1.java,然后将类的内容复制到它们中(不会对当前给定的代码进行任何更改),并按顺序编译:

D:\>javac Jpak02\PTaccess_modes.java

D:\>javac Jpak02\PTaccess_modes_1.java

编译第二个类时,我得到了这个(预期的)错误:

Jpak02\PTaccess_modes_1.java:9: error: d has private access in PTaccess_modes
        System.out.println("\nPrivate int d : " + d);
                                                  ^
1 error

在此之后,我删除了PTaccess_modes.java文件生成的.class文件并再次编译(这次使用private d字段在该行上添加了注释):

D:\>del Jpak02\PTaccess_modes.class

D:\>javac Jpak02\PTaccess_modes_1.java

编译并生成两个.class文件(如预期的那样)。


然后,最后但并非最不重要的是,我访问了包并从那里编译了类:

D:\>cd Jpak02

D:\Jpak02>javac PTaccess_modes_1.java

现在,我遇到了你说的问题:

PTaccess_modes_1.java:3: error: cannot find symbol
class PTaccess_modes_1 extends PTaccess_modes {
                               ^
  symbol: class PTaccess_modes
PTaccess_modes_1.java:6: error: cannot find symbol
        System.out.println("\nPublic int a : " + a);
                                                 ^
  symbol:   variable a
  location: class PTaccess_modes_1
PTaccess_modes_1.java:7: error: cannot find symbol
        System.out.println("\nProtected int b : " + b);
                                                    ^
  symbol:   variable b
  location: class PTaccess_modes_1
PTaccess_modes_1.java:8: error: cannot find symbol
        System.out.println("\nDefault int c : " + c);
                                                  ^
  symbol:   variable c
  location: class PTaccess_modes_1
4 errors

TL; DR:确保从正确的位置编译类并使用它们的完整名称,包括包名称(如本答案的第一个和第二个块所示)。

答案 1 :(得分:0)

您不能在继承的类中使用变量d。它被声明为私有,这意味着它只能在它声明的类中使用,而不是其他地方。