我有一个名为Employee
的类,其中包含2个private
变量,还包含所需的构造函数和其他必要的方法:
public class Employee {
private String name;
private int id;
public Employee() {
name = " No Name!";
id = 00100;
}
public Employee(String n, int i) {
name = n;
id = i;
}
public Employee(Employee originalObject) {
name = originalObject.name;
id = originalObject.id;
}
public String getName() {
return name;
}
public int getID() {
return id;
}
public void setName(String newName) {
if (newName == null) {
System.out.println("Fatal Error setting employee name!");
System.exit(0);
} else {
name = newName;
}
}
public void setID(int newID) {
id = newID;
}
public String toString() {
return (name + " " + id);
}
public boolean equals(Employee otherEmployee) {
return (name.equals(otherEmployee.name)
&& id == otherEmployee.id);
}
}
此Employee
类扩展了另一个名为HourlyEmployee
的类。扩展类如下:
public class HourlyEmployee extends Employee {
private double wageRate;
private double hours;
public HourlyEmployee() {
super();
wageRate = 0;
hours = 0;
}
public HourlyEmployee(String na, int di, double wR, double h) {
super(na, di);
if (wR >= 0 || h >= 0) {
wageRate = wR;
hours = h;
} else {
System.out.println("Fatal Error!: creating illegal hourly employee");
}
System.exit(0);
}
public HourlyEmployee(HourlyEmployee originalObject) {
super(originalObject);
wageRate = originalObject.wageRate;
hours = originalObject.hours;
}
public double getRate() {
return wageRate;
}
public double getHours() {
return hours;
}
public double getPay() {
return wageRate * hours;
}
public void setRate(double newWR) {
if (newWR >= 0) {
wageRate = newWR;
} else {
System.out.println("Fatal Error: negative hours worked!");
System.exit(0);
}
}
public String toString() {
return (getName() + " " + getID() + "\n$" + wageRate + " per hour for " + hours + "hours");
}
}
在我完成这个类的编写之后,我编写了Demo类来测试这些类以及它将如何工作。
public class InheritanceDemo {
public static void main(String[] args) {
HourlyEmployee joe = new HourlyEmployee("Joe Worker", 281952, 50.50, 160);
System.out.println("joe's longer name is " + joe.getName());
System.out.println("Changing joe's name to joseph.");
joe.setName("Joseph");
System.out.println("joe's record is as follows: ");
System.out.println(joe);
}
}
最后,我尝试编译代码并且工作正常,但不幸的是,即使它向我显示屏幕中的“进程已完成”,但是演示类仍未运行!您觉得这个程序有什么问题?
答案 0 :(得分:5)
问题在于System.exit(0)
中的第一个HourlyEmployee
。由于{}
之后没有else
,因此无条件执行退出。
答案 1 :(得分:3)
您的构造函数中不应包含System.exit。
您也不应该写出错误消息。抛出IllegalArgumentException。
你的HourlyEmployee ctor被塞了。这就是你想要的:
public class HourlyEmployee extends Employee {
private double wageRate;
private double hours;
public HourlyEmployee() {
super();
wageRate = 0;
hours = 0;
}
public HourlyEmployee(String na, int di, double wR, double h) {
super(na, di);
if (wR < 0) throw new IllegalArgumentException("wage rate cannot be negative");
if (h < 0) throw new IllegalAccessError("hours cannot be negative");
wageRate = wR;
hours = h;
}
public HourlyEmployee(HourlyEmployee originalObject) {
super(originalObject);
wageRate = originalObject.wageRate;
hours = originalObject.hours;
}
public double getRate() {
return wageRate;
}
public double getHours() {
return hours;
}
public double getPay() {
return wageRate * hours;
}
public void setRate(double newWR) {
if (newWR < 0) throw new IllegalArgumentException("wage rate cannot be negative");
wageRate = newWR;
}
public String toString() {
return (getName() + " " + getID() + "\n$" + wageRate + " per hour for " + hours + "hours");
}
}