不好意思, 问题问错了。。准备建立一个package 叫作 Shapes
在这个 package里面, 先写了个super class叫BasicShape:
package Shapes;
public class BasicShape{
...
}
然后在同样的directory 下, 写了个叫Shape2D的class继承BasicShape:
package Shapes; // 原来的post里写成package BasicShape了, 笔误
public class Shape2D extends BasicShape{
...
}
但当我compile Shape2D.java的时候, 说找不到class BasicShape,
不知道是什么原因, 谢谢解答
应该是目录的问题
应该把两个.java文件都放在Shapes的目录里。package的结构应该和文件目录结构一样。javac和java都应该在最上一级目录里执行,即Shapes的上一级目录。正确的结构应该是这样的:
C:\home>dir
Volume in drive C has no label.
Volume Serial Number is 43C1-842F
Directory of C:\home
06/23/2006 12:06 AM <DIR> .
06/23/2006 12:06 AM <DIR> ..
06/23/2006 12:06 AM <DIR> Shapes
0 File(s) 0 bytes
3 Dir(s) 32,119,504,896 bytes free
C:\home>dir Shapes
Volume in drive C has no label.
Volume Serial Number is 43C1-842F
Directory of C:\home\Shapes
06/23/2006 12:06 AM <DIR> .
06/23/2006 12:06 AM <DIR> ..
06/23/2006 12:08 AM 0 BasicShape.java
06/23/2006 12:08 AM 0 Shape2D.java
2 File(s) 0 bytes
2 Dir(s) 32,119,504,896 bytes free
C:\home>javac Shapes\Shape2D.java
......
C:\home>java Shapes.Shape2D
......
当你编译Shapes\Shape2D.java时,javac会去找Shapes\Shape2D.class,找不到的话就会自动去找Shapes\Shape2D.java来产生Shapes\Shape2D.class。你遇到的问题就是javac无法找到Shapes\Shape2D.java,因为你没有把Shape2D.java放在Shapes\里。
C:\home>dir
Volume in drive C has no label.
Volume Serial Number is 43C1-842F
Directory of C:\home
06/23/2006 12:06 AM <DIR> .
06/23/2006 12:06 AM <DIR> ..
06/23/2006 12:06 AM <DIR> Shapes
0 File(s) 0 bytes
3 Dir(s) 32,119,504,896 bytes free
C:\home>dir Shapes
Volume in drive C has no label.
Volume Serial Number is 43C1-842F
Directory of C:\home\Shapes
06/23/2006 12:06 AM <DIR> .
06/23/2006 12:06 AM <DIR> ..
06/23/2006 12:08 AM 0 BasicShape.java
06/23/2006 12:08 AM 0 Shape2D.java
2 File(s) 0 bytes
2 Dir(s) 32,119,504,896 bytes free
C:\home>javac Shapes\Shape2D.java
......
C:\home>java Shapes.Shape2D
......
当你编译Shapes\Shape2D.java时,javac会去找Shapes\Shape2D.class,找不到的话就会自动去找Shapes\Shape2D.java来产生Shapes\Shape2D.class。你遇到的问题就是javac无法找到Shapes\Shape2D.java,因为你没有把Shape2D.java放在Shapes\里。