
请java高手帮我改一下以下代码(为了增加代码的灵活性,需要输入工作天数和生产的零件数而不是直接赋值)
publicabstractclassEmployee{publicabstractdoublecalc();publicabstractdoubleearnings()...
public abstract class Employee{
public abstract double calc();
public abstract double earnings();
}
public class NormalWorker extends Employee {
int workdays;
public NormalWorker(int workdays) {
this.workdays=workdays;
}
public double calc(){
double workloads;
if(workdays>25){
workloads=workdays*1.5+10;
}
else{
workloads=workdays*1.5;
}
return workloads;
}
public double earnings(){
return this.calc()*30;
}
}
public class PieceWorker extends Employee{
int products;
public PieceWorker(int products){
this.products=products;
}
public double calc(){
double workloads;
if(products>40){
workloads=products*0.8+(products-40)*0.5;
}
else{
workloads=products*0.8;
}
return workloads;
}
public double earnings(){
return this.calc()*15;
}
}
public class Test {
public static void main(String args[]){
Employee e1=new NormalWorker(26);
Employee e2=new NormalWorker(50);
Employee e3=new PieceWorker(15);
Employee e4=new PieceWorker(40);
System.out.println("普通员工1工资为:"+e1.earnings());
System.out.println("普通员工2工资为:"+e2.earnings());
System.out.println("计件员工1工资为:"+e3.earnings());
System.out.println("计件员工2工资为:"+e4.earnings());
}
}
怎样加入Scanner reader=new Scanner(System.in);
int workdays=reader.nextInt();
int products=reader.nextInt();等可以修改变量(变量为NoramlWorker类里的workdays和PieceWorker里的products)的语句?
每个变量各输入2个值 展开
public abstract double calc();
public abstract double earnings();
}
public class NormalWorker extends Employee {
int workdays;
public NormalWorker(int workdays) {
this.workdays=workdays;
}
public double calc(){
double workloads;
if(workdays>25){
workloads=workdays*1.5+10;
}
else{
workloads=workdays*1.5;
}
return workloads;
}
public double earnings(){
return this.calc()*30;
}
}
public class PieceWorker extends Employee{
int products;
public PieceWorker(int products){
this.products=products;
}
public double calc(){
double workloads;
if(products>40){
workloads=products*0.8+(products-40)*0.5;
}
else{
workloads=products*0.8;
}
return workloads;
}
public double earnings(){
return this.calc()*15;
}
}
public class Test {
public static void main(String args[]){
Employee e1=new NormalWorker(26);
Employee e2=new NormalWorker(50);
Employee e3=new PieceWorker(15);
Employee e4=new PieceWorker(40);
System.out.println("普通员工1工资为:"+e1.earnings());
System.out.println("普通员工2工资为:"+e2.earnings());
System.out.println("计件员工1工资为:"+e3.earnings());
System.out.println("计件员工2工资为:"+e4.earnings());
}
}
怎样加入Scanner reader=new Scanner(System.in);
int workdays=reader.nextInt();
int products=reader.nextInt();等可以修改变量(变量为NoramlWorker类里的workdays和PieceWorker里的products)的语句?
每个变量各输入2个值 展开
2个回答
展开全部
public class Test {
public static void main(String args[]){
Scanner reader=new Scanner(System.in);
int workdays=reader.nextInt();
Employee e1=new NormalWorker(workdays);
workdays=reader.nextInt();
Employee e2=new NormalWorker(workdays);
int products=reader.nextInt();
Employee e3=new PieceWorker(products);
products=reader.nextInt();
Employee e4=new PieceWorker(products);
System.out.println("普通员工1工资为:"+e1.earnings());
System.out.println("普通员工2工资为:"+e2.earnings());
System.out.println("计件员工1工资为:"+e3.earnings());
System.out.println("计件员工2工资为:"+e4.earnings());
}
}
public static void main(String args[]){
Scanner reader=new Scanner(System.in);
int workdays=reader.nextInt();
Employee e1=new NormalWorker(workdays);
workdays=reader.nextInt();
Employee e2=new NormalWorker(workdays);
int products=reader.nextInt();
Employee e3=new PieceWorker(products);
products=reader.nextInt();
Employee e4=new PieceWorker(products);
System.out.println("普通员工1工资为:"+e1.earnings());
System.out.println("普通员工2工资为:"+e2.earnings());
System.out.println("计件员工1工资为:"+e3.earnings());
System.out.println("计件员工2工资为:"+e4.earnings());
}
}
展开全部
先给出提示语句,如System.out.println("请输入普通员工1的工作时间:")
再用对象的值域接收e1.workdays = reader.nextInt();这样啊
好像看你workdays是私有的,那你可以这样:
System.out.println("请输入普通员工1的工作时间:");
Employee e1=new NormalWorker(reader.nextInt());
每次用输入的值来new一个对象
再用对象的值域接收e1.workdays = reader.nextInt();这样啊
好像看你workdays是私有的,那你可以这样:
System.out.println("请输入普通员工1的工作时间:");
Employee e1=new NormalWorker(reader.nextInt());
每次用输入的值来new一个对象
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询