TestAccess.java
public class TestAccess {
private int i = 0;
int j = 0;
protected int k = 0;
public int m = 0;

public void m() {
i = 9;
}
}TestP.java
package m;public class TestP extends TestAccess {
public void mthod() {
System.out.println(k);
}
}
两个文件位于同一个目录下,编译第一个文件通过,编译第二个文件时报错,总是提示找不到符号 TestAccess.但是如果去掉package m;这条语句则编译通过,这个是什么原因呢,请高手指点。

解决方案 »

  1.   

    不在一个包下  要import
      

  2.   

    为了防止不同包中同名类的冲突,所以需要将不再同一包内的类导入!也就是import
      

  3.   

    要在TestP类外面import TestAccess 类
      

  4.   

    TestAccess.java里面打包package pkg;
    TestP.java里面引用 import pkg;
      

  5.   

    要么是第二个文件要放在m包中,要么要import第一个类
      

  6.   

    你们没看懂我的意思, 现在两个文件时放在同一个目录下,编译TestAccess通过,TestP第一行打包,同事TestP继承了TestAccess,在编译TestP的时候就会报错,是编译时就出错。 但是去掉package m;编译则通过。至于楼上的朋友说引入TestAccess。这个如何引入,这是个裸体类,根本没有包。
      

  7.   

    从 J2SE 1.4 开始,Java 编译器不再支持 import 进未命包名的类、接口。详见 J2SE 1.4 与 J2SE 1.3 兼容性文档,第 8 点第二部分:http://java.sun.com/javase/compatibility_j2se1.4.htmlThe compiler now rejects import statements that import a type from the unnamed namespace. Previous versions of the compiler would accept such import declarations, even though they were arguably not allowed by the language (because the type name appearing in the import clause is not in scope). The specification is being clarified to state clearly that you cannot have a simple name in an import statement, nor can you import from the unnamed namespace.To summarize, the syntax    import SimpleName;is no longer legal. Nor is the syntax    import ClassInUnnamedNamespace.Nested;which would import a nested class from the unnamed namespace. To fix such problems in your code, move all of the classes from the unnamed namespace into a named namespace. 
      

  8.   

    没说引包,import TestAccess;应该就行,这个类在默认包下了 不是没有包,前提是TestAccess类与m包在同一父目录目录下,这样JVM才能识别,另外我觉得你不import就是编译时报错。