java - 为什么继承类方法在 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());
解决方案
问题是:
HourlyEmployee
empId
对字段和name
超类一无所知Employee
,因为它们是私有的。中的同名字段HourlyEmployee
实际上与那些字段无关,它们是完全独立的。HourlyEmployee
不会覆盖这些字段的 setter 和 getter,因此它们仍然引用基类中的字段Employee
。- 的构造函数
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;
}
}
推荐阅读
- apache-camel - 在 activeMQs 嵌入式 Camel 模块中找不到 Camel Zookeeper 组件
- ubuntu - Win10 Guest 无法通过桥接无线网络接口 ping VMWARE 播放器上的 Ubuntu 主机
- javascript - 使用 Javascript,如何解析包含 JSON 字符串的 JSON 字符串?
- javascript - THREE.LoopOnce 不播放动画
- apache-spark - DAG 中的 ExternalRDDScan 是什么?
- c++ - 当方法不修改成员时,是否在 const 实例上调用非常量方法?
- xcode - increment_version_number 在工作区中不起作用
- python - 读取 csv 文件:列表索引超出范围
- python - 从自定义预训练模型中删除层
- css - 带有导航栏的 reactJS 应用程序不能正确对齐下拉菜单项