为什么继承 class 方法在 Java 中打印 0 和 null?
Why the inheritance class methods print 0 and null in Java?
我有一个员工class(包括字段:empId、姓名,方法:getEmpId、getName、setEmpId、setName)。我从 Employee 创建了一个名为 HourlyEmployee 的继承 class。 HourlyEmployee class 有附加字段:费率、小时数和附加方法: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());
如果你扩展 class,你不应该 re-declare 字段
您在 Employee
private int empId;
private String name;
不要re-declare在HourlyEmployee
还有,为什么是这个classstatic
?
问题是:
HourlyEmployee
对 superclass Employee
的字段 empId
和 name
一无所知,因为它们是私有的。 HourlyEmployee
中的同名字段实际上与那些无关,它们是完全独立的。
HourlyEmployee
不会覆盖这些字段的 setter 和 getter,因此它们仍然引用基础 class 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;
}
}
你不应该在 HourlyEmployee 中声明这两个字段 class。
private int empId;
private String name;
当您调用 setEmpId
和 setName
时,您正在调用在 Employee
中设置 empId
和 name
的 Employee 方法。但是,当您调用 employeeInfo
时,您在 HourlyEmployee 中显示 empId
和 name
,这与 Employee 的 empid
和 name
是两个完全不同的字段。如果 int
和 String
.
,则 0 和 null 是默认值
我有一个员工class(包括字段:empId、姓名,方法:getEmpId、getName、setEmpId、setName)。我从 Employee 创建了一个名为 HourlyEmployee 的继承 class。 HourlyEmployee class 有附加字段:费率、小时数和附加方法: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());
如果你扩展 class,你不应该 re-declare 字段
您在 Employee
private int empId;
private String name;
不要re-declare在HourlyEmployee
还有,为什么是这个classstatic
?
问题是:
HourlyEmployee
对 superclassEmployee
的字段empId
和name
一无所知,因为它们是私有的。HourlyEmployee
中的同名字段实际上与那些无关,它们是完全独立的。HourlyEmployee
不会覆盖这些字段的 setter 和 getter,因此它们仍然引用基础 classEmployee
. 中的字段
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;
}
}
你不应该在 HourlyEmployee 中声明这两个字段 class。
private int empId;
private String name;
当您调用 setEmpId
和 setName
时,您正在调用在 Employee
中设置 empId
和 name
的 Employee 方法。但是,当您调用 employeeInfo
时,您在 HourlyEmployee 中显示 empId
和 name
,这与 Employee 的 empid
和 name
是两个完全不同的字段。如果 int
和 String
.