【问题标题】:Why the inheritance class methods print 0 and null in Java?为什么继承类方法在 Java 中打印 0 和 null?
【发布时间】:2020-09-25 03:39:14
【问题描述】:

我有一个 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());

【问题讨论】:

  • 通过重新声明具有相同名称的字段,您正在创建单独的变量(即使它们具有相同的名称)。不要那样做。不再声明名称和 ID,而是再次将特定值传递给超类中的字段。在构造函数中,您可以通过super(empId, name); 实现它。此外,由于超类型中的这些字段对于 get 是私有的,因此它们的值使用 getter 方法,例如 System.out.println("The employee's id is: " + getEmpId());.
  • 非常感谢。学习 super() 真是太好了。

标签: java class inheritance methods null


【解决方案1】:

问题是:

  1. HourlyEmployee 不知道超类Employee 的字段empIdname,因为它们是私有的。 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;
    }
}

【讨论】:

  • 非常感谢您这么具体的解释。它按预期工作得很好。之前不知道super(),好好学习。
【解决方案2】:

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

private int empId;
private String name;

当您调用setEmpIdsetName 时,您调用的是在Employee 中设置empIdname 的Employee 方法。但是,当您调用 employeeInfo 时,您会在 HourlyEmployee 中显示 empIdname,这是与 Employee 的 empidname 完全不同的两个字段。如果intString,则0和null是默认值。

【讨论】:

  • 知道了。非常感谢。
【解决方案3】:

如果你扩展一个类,你不应该重新声明字段

你有Employee

private int empId;
private String name; 

不要在HourlyEmployee重新声明

还有,为什么这个类是static

【讨论】:

    猜你喜欢
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多