首页 > 解决方案 > 为什么继承类方法在 Java 中打印 0 和 null?

问题描述

我有一个 Employee 类(包括字段:empId、name、方法:getEmpId、getName、setEmpId、setName)。我从 Employee 创建了一个名为 HourlyEmployee 的继承类。HourlyEmployee 类有附加字段:rate、hours 和附加方法:setRate、setHours、monthlyPayment。我创建了一个名为 he1 的 HourlyEmployee 对象,当调用 setEmpId 和 setName 时,它​​会打印 0 和 null。你能告诉我我的代码有什么问题吗?谢谢。

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());

标签: javaclassinheritancemethodsnull

解决方案


问题是:

  1. HourlyEmployeeempId对字段和name超类一无所知Employee,因为它们是私有的。中的同名字段HourlyEmployee实际上与那些字段无关,它们是完全独立的。
  2. HourlyEmployee不会覆盖这些字段的 setter 和 getter,因此它们仍然引用基类中的字段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;
    }
}

推荐阅读