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个回答
展开全部
一个 *.java 文件中,只能有一个public 的类,而且这个public修饰的这类必需要和这个文件名相同.
//BOSS.java
public final class Boss extends Employee{ }
你把public去掉就可以了.
//BOSS.java
public final class Boss extends Employee{ }
你把public去掉就可以了.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
编译(JAVAC)时,出现这一错误是因为:你的新建JAVA程序虽然用了新的文件名,但在程序中的类(CLASS)名,用了其它程序中已有的类名(可能是在原来已存在的别的文件中)。解决办法:只要将新建文件中的类名改成与你的新建文件名一样的就可以了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把每个class写在一个以它的class名字为文件名的文件里,放在同一个文件夹里就可以了...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
错误说的很清楚。
Java源文件中pulic类/接口等只能存在一个,且文件名必须与其一致(大小写敏感),你试着将各个pulic类型的文件名改成类型名一致再试下。
另外,你这个提问分类怎么跑这儿来了
Java源文件中pulic类/接口等只能存在一个,且文件名必须与其一致(大小写敏感),你试着将各个pulic类型的文件名改成类型名一致再试下。
另外,你这个提问分类怎么跑这儿来了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询