为什么在Java中继承的类方法会打印出0和null?

huangapple go评论74阅读模式
英文:

Why the inheritance class methods print 0 and null in Java?

问题

我有一个员工类包括字段empIdname方法getEmpIdgetNamesetEmpIdsetName)。我创建了一个继承类称为HourlyEmployee继承自EmployeeHourlyEmployee类有额外的字段ratehours以及额外的方法setRatesetHoursmonthlyPayment我创建了一个HourlyEmployee对象名为he1当调用setEmpId和setName时它打印出0和null你能告诉我代码有什么问题吗谢谢

public class Employee {
    // 实例变量
    private int empId;
    private String name;

    // 构造函数
    public Employee() {

    }

    public Employee(int empId, String name) {
        this.empId = empId;
        this.name = name;
    }

    // 获取方法
    public int getEmpId() {
        return empId;
    }

    public String getName() {
        return name;
    }

    // 设置方法
    public void setEmpId(int empId) {
        this.empId = empId;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public static class HourlyEmployee extends Employee {
    private double rate;
    private double hours;

    public HourlyEmployee() {
    }

    public HourlyEmployee(int empId, String name, double rate, double hours) {
        super(empId, name);
        this.rate = rate;
        this.hours = hours;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }

    public void setHours(double hours) {
        this.hours = hours;
    }

    public double monthlyPayment() {
        double monthlyPayment = rate * hours;
        return monthlyPayment;
    }

    public void employeeInfo() {
        System.out.println("员工的编号是:" + getEmpId());
        System.out.println("员工的姓名是:" + getName());
        System.out.println("每小时工资是:" + rate);
        System.out.println("员工工作了 " + hours + " 小时");
    }
}

public static class Test {

    public static void main(String[] args) {
        double rate;
        double hours;

        // 创建一个无参构造函数的HourlyEmployee对象
        HourlyEmployee he1 = new HourlyEmployee();
        // 设置实例变量
        he1.setEmpId(1);
        he1.setName("Jane");
        he1.setRate(15.0);
        he1.setHours(50.0);

        // 显示员工信息
        he1.employeeInfo();
        // 显示月薪
        System.out.println("修改前的月薪为:" + he1.monthlyPayment());

        // 修改工资率
        he1.setRate(22.0);
        // 显示月薪
        System.out.println("修改后的月薪为:" + he1.monthlyPayment());

注意:我已经进行了必要的代码调整,以解决字段重复和构造函数问题。

英文:

I have an Employee class (including fields: empId, name, methods: getEmpId, getName, setEmpId, setName). And I create an inheritance class called HourlyEmployee from Employee. HourlyEmployee class has additional fields: rate, hours, and additional methods: setRate, setHours, monthlyPayment. I create an HourlyEmployee object called he1, when setEmpId and setName are invoked, it prints 0 and null. Can you tell me what's wrong with my code? Thanks.

public class Employee {
// instance variables
private int empId;
private String name;
// constructor
public Employee(){
}
public Employee(int empId, String name) {
this.empId = empId;
this.name = name;
}
// get methods
public int getEmpId() {
return empId;
}
public String getName() {
return name;
}
// set methods
public void setEmpId(int empId) {
this.empId = empId;
}
public void setName(String name) {
this.name = name;
}
public static class HourlyEmployee extends Employee {
private int empId;
private String name;
private double rate;
private double hours;
public HourlyEmployee() {
}
public HourlyEmployee (int empId, String name, double rate, double hours) {
this.empId = empId;
this.name = name;
this.rate = rate;
this.hours = hours;
}
public void setRate(double rate) {
this.rate = rate;
}
public void setHours(double hours) {
this.hours = hours;
}
public double monthlyPayment(){
double monthlyPayment = rate * hours;
return monthlyPayment;
}
public void employeeInfo(){
System.out.println("The employee's id is: " + empId);
System.out.println("The employee's name is: " + name);
System.out.println("The hourly rate is: " + rate);
System.out.println("The employee worked " + hours + " for a month");
}
}
public class Test {
public static void main(String[] args) {
int id;
String name;
double rate;
double hours;
double salary;
// create an hourly employee object with no-arg constructor
HourlyEmployee he1 = new HourlyEmployee ();
// set instance variables
he1.setEmpId(1);
he1.setName("Jane");
he1.setRate(15.0);
he1.setHours(50.0);
// display employee info
he1.employeeInfo();
// display monthly payment
System.out.println("Monthly payment, before change, is: " + he1.monthlyPayment());
// change the rate
he1.setRate(22.0);
// display monthly payment
System.out.println("Monthly payment, after change, is: " + he1.monthlyPayment());

答案1

得分: 4

问题在于:

  1. HourlyEmployee 不知道超类 Employee 的私有字段 empIdname 的存在,因为它们是私有的。HourlyEmployee 中具有相同名称的字段实际上与其无关,它们是完全独立的。
  2. HourlyEmployee 没有覆盖这些字段的设置器和获取器,因此它们仍然引用基类 Employee 中的字段。
  3. HourlyEmployee 的构造函数没有显式地调用超类构造函数,因此隐式地调用了默认构造函数。

如果这些字段应该始终被明确设置,我建议从 Employee 中删除默认构造函数。而且 HourlyEmployee 应该使用接受这些参数的构造函数来初始化这些字段:

public static class HourlyEmployee extends Employee {
    private double rate;
    private double hours;

    public HourlyEmployee(int empId, String name, double rate, double hours) {
        super(empId, name);
        this.rate = rate;
        this.hours = hours;
    }
}
英文:

The problem is:

  1. HourlyEmployee does not know anything about the fields empId and name of the superclass Employee, as they are private. The fields with the same names in HourlyEmployee are actually unrelated to those, they are completely separate.
  2. HourlyEmployee does not override the setters and the getters for these fields, so they still refer to the fields in the base class Employee.
  3. The constructor of HourlyEmployee does not explicitly invoke a super constructor, so the default constructor is implicitly invoked.

I'd recommend to remove the default constructor from Employee if these fields should always be set explicitly. And HourlyEmployee should initialize these fields using the constructor that accepts these parameters:

public static class HourlyEmployee extends Employee {
private double rate;
private double hours;
public HourlyEmployee (int empId, String name, double rate, double hours) {
super(empId, name);
this.rate = rate;
this.hours = hours;
}
}

答案2

得分: 2

你不应该在HourlyEmployee类中声明这两个字段。

private int empId;
private String name;

当你调用setEmpIdsetName时,你实际上是在调用Employee类的方法,这些方法会设置Employee类中的empIdname。但是当你调用employeeInfo方法时,你在显示HourlyEmployee类中的empIdname,这两个字段与Employee类中的empIdname完全不同。而且,0和null分别是intString的默认值。

英文:

You should not declare this two fiels in HourlyEmployee class.

private int empId;
private String name;

When you call setEmpId and setName, you are calling Employee's method which set empId and name in Employee. But when you call employeeInfo, you are displaying empId and name in HourlyEmployee which is two totally different field from Employee's empid and name. And the 0 and null is the default value if int and String.

答案3

得分: 1

如果您扩展一个类,那么就不应该重新声明字段。

Employee 类中您有:

private int empId;
private String name;

HourlyEmployee 类中不要重新声明。

另外,为什么这个类是 static 的呢?

英文:

If you extend a class, the you shouldn't re-declare fields

You have in Employee

private int empId;
private String name; 

Do not re-declare in HourlyEmployee

Also, why are this class static ?

huangapple
  • 本文由 发表于 2020年9月25日 11:39:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64057454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定