java 类是公共的,应在名为.java 的文件中声明

//源代码保存为EmployeeTest.javaimportjavax.swing.JOptionPane;importjava.text.DecimalFormat;... //源代码保存为EmployeeTest.java
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

//EmployeeTest.java
public class EmployeeTest{
public static void main(String[] args){
Employee ref;
String output="";

Boss b=new Boss("史","季华",800.00);
CommissionWorker c=new CommissionWorker("张","雪",400.0,3.0,150);

DecimalFormat precision2=new DecimalFormat("0.00");

ref=b; //把父类的引用ref赋值为子类经理boss对b的引用
output+=ref.toString()+"工资(元)"+
precision2.format(ref.earnings())+"\n"+
b.toString()+"工资(元)"+
precision2.format(b.earnings())+"\n";

ref=c;
output+=ref.toString()+"工资(元)"+
precision2.format(ref.earnings())+"\n"+
c.toString()+"工资(元)"+
precision2.format(c.earnings())+"\n";

JOptionPane.showMessageDialog(null,output,
"Demonstrating polymorphism",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}

//Employee.java
public abstract class Employee{
private String firstName;
private String lastName;
public Employee(String first,String last){
firstName=first;
lastName=last;
}
public String getFirstName(){return firstName;}
public String getLastName(){return lastName;}
public String toString(){return firstName+' '+lastName;}
//Employee抽象方法earnings();
public abstract double earnings();
}
//BOSS.java
public final class Boss extends Employee{
private double weeklySalary;
public Boss(String first,String last,double s){
super(first,last);
setWeeklySalary(s);
}
public void setWeeklySalary(double s){
weeklySalary=(s>0?s:0);}
public double earnings(){return weeklySalary;}
public String toString(){
return "经理:"+super.toString();}
}
//CommissionWorker.java
public final class CommissionWorker extends Employee{
private double salary; //每周的底薪
private double commission; //每周奖金
private double quantity; //销售额
public CommissionWorker(String first,String last,double s,double c,double q){
super(first,last);
setSalary(s);
setCommission(c);
setQuantity(q);
}
public void setSalary(double s){
salary=(s>0?s:0);
}
public void setCommission(double c){
commission=(c>0?c:0);
}
public void setQuantity(double q){
quantity=(q>0?q:0);
}
public double earnings(){
return salary+commission*quantity;
}
public String toString(){
return "普通员工:"+super.toString();
}
}
错误:D:\java>javac EmployeeTest.java
EmployeeTest.java:42: 类 Employee 是公共的,应在名为 Employee.java 的文件中声明
public abstract class Employee{
^EmployeeTest.java:63: 类 Boss 是公共的,应在名为 Boss.java 的文件中声明
public final class Boss extends Employee{
^EmployeeTest.java:82: 类 CommissionWorker 是公共的,应在名为 CommissionWorker.ja
va 的文件中声明
public final class CommissionWorker extends Employee{
^3 错误

不好意思,新手编程希望能得到大家的帮助!~,怎么解决好呢,是把几个public类分写到另外的文件中?还是有其他办法呢··谢谢了
展开
 我来答
kimserlam
2008-08-14
知道答主
回答量:5
采纳率:0%
帮助的人:0
展开全部
同一个包中可以不用public 权限啊,你用private权限。。要么你就放到另外的文件中。。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式