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类分写到另外的文件中?还是有其他办法呢··谢谢了 展开
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类分写到另外的文件中?还是有其他办法呢··谢谢了 展开
6个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询