将params传递给超级构造函数时遇到问题。我正在实现带有继承的抽象。我创建了三个扩展抽象类 Employee 的类。但是当我创建这三个类的对象时,只有一个类将参数正确传递给super()构造函数,而其余类将返回null。 这是抽象类
public abstract class Employee {
private String firstName,lastName,SSN;
public Employee(String fName, String lName, String SSN){
this.SSN = SSN;
this.firstName = fName;
this.lastName = lName;
}
public void setFirstName(String fName){
this.firstName = firstName;
}
public void setLastName(String lName){
this.lastName = lName;
}
public void setSSN(String SSN){
this.SSN = SSN;
}
public String getSSN(){
return SSN;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public abstract double getEarnings();
@Override
public String toString(){
return String.format("First Name: %-2s \n Last Name: %-2s \n Social Security Number: %-2s", getFirstName(), getLastName(),getSSN());
}
以下是课程:
public class SalariedEmployee extends Employee {
private String firstName, lastName, SSN;
double weeklyPay;
public SalariedEmployee(String fName, String lName, String SSN, double wPay) {
super(fName, lName, SSN);
this.weeklyPay = wPay;
}
public void setWeeklyPay(double wPay) {
this.weeklyPay = wPay;
}
public double getWeeklyPay() {
return weeklyPay;
}
@Override
public double getEarnings() {
return getWeeklyPay();
}
@Override
public String toString() {
return String.format("%s \n Weekly Pay: %2s", super.toString(), getEarnings());
}
}
public class HourlyEmployee extends Employee{
private String firstName, lastName, SSN;
private double hourlyPay,wage;
private int hours;
public HourlyEmployee(String fName, String lName, String SSN, int hour, double hourlyPay ){
super(fName, lName, SSN);
this.hours = hour;
this.hourlyPay = hourlyPay;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSSN() {
return SSN;
}
public void setSSN(String SSN) {
this.SSN = SSN;
}
public double getHourlyPay() {
return hourlyPay;
}
public void setHourlyPay(double hourlyPay) {
this.hourlyPay = hourlyPay;
}
public double getWage() {
return wage;
}
public void setWage(double wage) {
this.wage = wage;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
@Override
public double getEarnings(){
double earnings;
if(hours> 40)
earnings = hourlyPay * 40 + ((hours-40) *hourlyPay *1.5);
else
earnings = hourlyPay * hours;
return earnings;
}
@Override
public String toString(){
return String.format("%s \n Hours worked: %s \n Salary: %s \n ", super.toString(), getHours(), getEarnings());
}
}
public class CommissionEmployee extends Employee {
private String firstName, lastName, SSN;
private double sales,commission;
public CommissionEmployee(String fName, String lName, String SSN, double sales, double commission){
super(fName, lName, SSN);
this.commission = commission;
this.sales = sales;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSSN() {
return SSN;
}
public void setSSN(String SSN) {
this.SSN = SSN;
}
public double getSales() {
return sales;
}
public void setSales(double sales) {
this.sales = sales;
}
public double getCommission() {
double commissionRate;
if(commission >0 && commission<1)
return commission;
else
return 0;
}
public void setCommission(double commission) {
this.commission = commission;
}
@Override
public double getEarnings(){
return getCommission() * getSales();
}
@Override
public String toString(){
return String.format("%s \n Commission Rate: %s \n Salary: %s \n", super.toString(), getCommission(), getEarnings());
}
}
这是TEST课程
public class EmployeeTest {
public static void main(String[] args) {
Employee salariedEmp,hourlyEmp,commissionEmp;
salariedEmp = new SalariedEmployee("Aminu", "Bishir", "11-22-33-33", 300.47);
System.out.println(printDetails(salariedEmp));
hourlyEmp = new HourlyEmployee("Musa","Sale","22-33-44-55",50,20.4);
System.out.println(printDetails(hourlyEmp));
commissionEmp = new CommissionEmployee("Muhammad","Ibrahim","33-44-55-66",20000,0.3);
System.out.print( printDetails(commissionEmp));
}
public static String printDetails(Employee emp){
return String.format("%s \n %s",emp.getClass(),emp.toString());
}
}
这是我每次运行时得到的输出:
class Abstraction.SalariedEmployee First Name: Aminu Last Name: Bishir Social Security Number: 11-22-33-33 Weekly Pay: 300.47 class Abstraction.HourlyEmployee First Name: null Last Name: null Social Security Number: null Hours worked: 50 Salary: 1122.0 class Abstraction.CommissionEmployee First Name: null Last Name: null Social Security Number: null Commission Rate: 0.3 Salary: 6000.0
答案 0 :(得分:1)
你应该删除行
private String firstName, lastName, SSN;
在子课程CommissionEmployee
,HourlyEmployee
和SalariedEmployee
中。因为这些属性是在抽象类Employee
中声明的,所以请使用它。
答案 1 :(得分:0)
变量
private String firstName,lastName,SSN;
已在您的员工类中定义,无需重复。这就是那些空值的原因
答案 2 :(得分:0)
多次声明变量时遇到问题。通过在超类中声明它们,它们也在子类中声明。当您在子类中声明具有相同名称的变量时,您可以解决超类的问题。通过super.variable
变量。但我建议你简单地删除子类中已经在超类中声明的变量声明。
然而,这不是问题的根源。如果覆盖方法会出现类似的问题,在您的情况下,正确打印的子类不会覆盖getter,getter用于读取存储在变量中的信息。在正确打印出来的类中,getter可以在超类中找到,因此getter正在处理超类变量。在其他2个类中,找到了重写的getter,它们使用在子类中找到的变量。由于您只在超类中设置变量,因此子类变量始终为null,因此结果也是如此。
我建议你删除子类中的重写getter和变量。你不需要它们。