英文:
Why the inheritance class methods print 0 and null in Java?
问题
我有一个员工类(包括字段:empId、name,方法:getEmpId、getName、setEmpId、setName)。我创建了一个继承类,称为HourlyEmployee,继承自Employee。HourlyEmployee类有额外的字段:rate、hours,以及额外的方法:setRate、setHours、monthlyPayment。我创建了一个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
问题在于:
HourlyEmployee
不知道超类Employee
的私有字段empId
和name
的存在,因为它们是私有的。HourlyEmployee
中具有相同名称的字段实际上与其无关,它们是完全独立的。HourlyEmployee
没有覆盖这些字段的设置器和获取器,因此它们仍然引用基类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;
}
}
英文:
The problem is:
HourlyEmployee
does not know anything about the fieldsempId
andname
of the superclassEmployee
, as they are private. The fields with the same names inHourlyEmployee
are actually unrelated to those, they are completely separate.HourlyEmployee
does not override the setters and the getters for these fields, so they still refer to the fields in the base classEmployee
.- 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;
当你调用setEmpId
和setName
时,你实际上是在调用Employee类的方法,这些方法会设置Employee类中的empId
和name
。但是当你调用employeeInfo
方法时,你在显示HourlyEmployee类中的empId
和name
,这两个字段与Employee类中的empId
和name
完全不同。而且,0和null分别是int
和String
的默认值。
英文:
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
?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论