Java继承中子类方法调用无法传递计算值问题求助
Java继承场景薪资计算错误的修复方案
核心问题梳理
- 继承关系颠倒:错误地将主程序类
RunEmployee1作为父类,正确设计应该是定义Employee基类,由全职、兼职子类继承并实现各自的薪资逻辑。 - static变量滥用:所有属性都用static修饰,导致不同对象共享同一数据;子类还重复定义同名static变量,遮蔽父类数据,造成输入值无法正确传递到输出。
- 方法逻辑混乱:set/get方法职责错位(比如set方法里还做控制台输入),输出方法存在语法错误(如
PartTimeEmployee1的writeOutput3里有多余加号)。 - 主程序逻辑冗余:不管用户选择全职还是兼职,都实例化所有子类并调用输出,导致无关的0.0薪资输出。
修正后的完整代码
1. 基类Employee(统一员工属性与行为)
public class Employee { protected String name; // 构造方法初始化姓名 public Employee(String name) { this.name = name; } // 抽象方法,子类必须实现自己的薪资计算 public abstract double getSalary(); // 输出员工基本信息 public void printBasicInfo() { System.out.println("Employee's name is: " + name); } }
2. 全职员工子类FullTimeEmployee
public class FullTimeEmployee extends Employee { private double monthlySalary; public FullTimeEmployee(String name, double monthlySalary) { super(name); // 调用父类构造方法初始化姓名 this.monthlySalary = monthlySalary; } @Override public double getSalary() { return monthlySalary; } // 输出全职薪资 public void printFullTimeSalary() { System.out.println("Employee's Full time Monthly Salary is: " + getSalary()); } }
3. 兼职员工子类PartTimeEmployee
public class PartTimeEmployee extends Employee { private int workedHours; private double hourlyRate; public PartTimeEmployee(String name, int workedHours, double hourlyRate) { super(name); this.workedHours = workedHours; this.hourlyRate = hourlyRate; } @Override public double getSalary() { return workedHours * hourlyRate; // 计算兼职薪资 } // 输出兼职薪资 public void printPartTimeSalary() { System.out.println("Employee's Part time Monthly Wage is: " + getSalary()); } }
4. 主程序类RunEmployee(处理输入与对象创建)
import java.util.Scanner; public class RunEmployee { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String name; char position; System.out.println("Enter Employee name: "); name = sc.nextLine(); System.out.println("Type F for Full time or P for Part time: "); System.out.println("Use Capital Letter only!"); position = sc.next().charAt(0); switch (position) { case 'F': System.out.println("You are a Full time Employee!"); System.out.println("Enter your Monthly Wage: "); double monthlySalary = sc.nextDouble(); FullTimeEmployee fullTimeEmp = new FullTimeEmployee(name, monthlySalary); fullTimeEmp.printBasicInfo(); fullTimeEmp.printFullTimeSalary(); break; case 'P': System.out.println("You are a Part time Employee!"); System.out.println("Enter the number of hours you worked in: "); int hours = sc.nextInt(); System.out.println("Enter the hourly rate you are working in: "); double rate = sc.nextDouble(); PartTimeEmployee partTimeEmp = new PartTimeEmployee(name, hours, rate); partTimeEmp.printBasicInfo(); partTimeEmp.printPartTimeSalary(); break; default: System.out.println("You are not an Employee!"); } sc.close(); } }
修复后效果
选择兼职(P),输入姓名Name、工时5、时薪100,输出如下:
Enter Employee name: Name
Type F for Full time or P for Part time: Use Capital Letter only!
P
You are a Part time Employee!
Enter the number of hours you worked in: 5
Enter the hourly rate you are working in: 100
Employee's name is: Name
Employee's Part time Monthly Wage is: 500.0
内容的提问来源于stack exchange,提问作者玉ねぎ忍者
相关产品推荐
相关产品推荐

