早教吧 育儿知识 作业答案 考试题库 百科 知识分享

javaeclipse为什么编译后显示ThepublictypeDollmustbedefinedinitsownfile我的程序:publicclassDoll{privateStringname;publicDoll(Stringname){this.name=name;}publicvoidspeak(){System.out.println(name);}}publicclassDoll

题目详情
java eclipse 为什么编译后显示The public type Doll must be defined in its own file
我的程序:
public class Doll{
private String name;
public Doll(String name) {
this.name = name;
}
public void speak(){
System.out.println(name);
}
}
public class DollApp{
public static void main(String[] args) {
// TODO Auto-generated method stub
Doll beibei = new Doll("贝贝");
Doll jingjing = new Doll("晶晶");
Doll huanhuan = new Doll("欢欢");
Doll yingying = new Doll("迎迎");
Doll nini = new Doll("妮妮");
beibei.speak();
jingjing.speak();
huanhuan.speak();
yingying.speak();
nini.speak();
}
}
太纠结了,
显示的错误信息是:
Exception in thread "main" java.lang.Error:Unresolved compilation problem:
The public type Doll must be defined in its own file
at Doll.(DollApp.java:2) 就是第一行 public class Doll{ 感觉是我Doll的命名问题
at DollApp.main(DollApp.java:19)
▼优质解答
答案和解析
两个类要分开写到不同的.java文件中