我有两个班级。当我将 TapeDeckTestDrive 类放在文本编辑器的第一位时,它运行良好。当我把 TestDrive 类放在第一位时,它给出了找不到主类的错误。为什么是这样?
class TapeDeck {
boolean canRecord = false;
void playTape(){
System.out.println("tape playing");
}
void recordTape(){
System.out.println("tape recording");
}
}
class TapeDeckcTestDrive{
public static void main(String[] args){
TapeDeck t = new TapeDeck();
t.canRecord = true;
t.playTape();
if (t.canRecord == true) {
t.recordTape();
}
}
}
此格式错误
VS
以下工作正常:
class TapeDeckcTestDrive{
public static void main(String[] args){
TapeDeck t = new TapeDeck();
t.canRecord = true;
t.playTape();
if (t.canRecord == true) {
t.recordTape();
}
}
}
class TapeDeck {
boolean canRecord = false;
void playTape(){
System.out.println("tape playing");
}
void recordTape(){
System.out.println("tape recording");
}
}
原文由 Abdul Hussain 发布,翻译遵循 CC BY-SA 4.0 许可协议
使用命令编译代码后:
通过仅指定
fileName
而不指定 ---.java
扩展名来运行 java.class
文件如果您使用
fileName.java
它不会运行特定的.class
文件;它将尝试解释.java
文件。如果要解释.java
文件,则父类必须包含main(String[])
方法。