我有3个班级:管理员,受薪员工和员工。管理员从受薪员工延伸,并从员工延伸。以下是它们的默认构造函数:
员工:
public class Employee
{
private String name;
private Date hireDate;
public Employee( )
{
name = "No name";
hireDate = new Date("Jan", 1, 1000); //Just a placeholder.
}
/**
Precondition: Neither theName nor theDate is null.
*/
public Employee(String theName, Date theDate)
{
if (theName == null || theDate == null)
{
System.out.println("Fatal Error creating employee.");
System.exit(0);
}
name = theName;
hireDate = new Date(theDate);
}
public Employee(Employee originalObject)
{
name = originalObject.name;
hireDate = new Date(originalObject.hireDate);
}
}
SalariedEmployee:
public class SalariedEmployee extends Employee
{
private double salary; //annual
public SalariedEmployee( )
{
super( );
salary = 0;
}
/**
Precondition: Neither theName nor theDate are null;
theSalary is nonnegative.
*/
public SalariedEmployee(String theName, Date theDate, double theSalary)
{
super(theName, theDate);
if (theSalary >= 0)
salary = theSalary;
else
{
System.out.println("Fatal Error: Negative salary.");
System.exit(0);
}
}
public SalariedEmployee(SalariedEmployee originalObject )
{
super(originalObject);
salary = originalObject.salary;
}
}
管理员:
//Create a test program that uses scanner so person can input values for the admin class.
public class Administrator extends SalariedEmployee
{
private String title;
private String responsibility;
private String supervisor;
//No argument Constructor
public Administrator( )
{
super( );
title = "No Title";
responsibility = "No Responsibility";
supervisor = "Dilbert";
}
//3 argument constructor
public Administrator(String theTitle, String theResponsibility, String theSupervisor)
{
super( );
if (theTitle == null || theResponsibility == null || theSupervisor == null)
{
System.out.println("Fatal Error creating employee.");
System.exit(0);
}
title = theTitle;
responsibility = theResponsibility;
supervisor = theSupervisor;
}
//6 argument Constructor
public Administrator(String theName, Date theDate, double theSalary, String theTitle, String theResponsibility, String theSupervisor)
{
super(theName,theDate,theSalary);
title = theTitle;
responsibility = theResponsibility;
supervisor = theSupervisor;
}
}
我尝试使用以下方法创建一个新的管理员对象:
Administrator admin = new Administrator();
但是我得到了一个致命的错误。我做错了什么?
答案 0 :(得分:2)
您的日期类输出"致命错误"处于非信息方式的任何地方(在你发布你的Date类代码之前我们都不知道)。
第一次出现致命错误位于setDate
方法内,因为dateOK
的{{1}}返回false。
您的"Jan", 1, 1000
方法需要monthOK
,因此您用于默认构造函数的"January"
无效。更改默认构造函数以使用
"Jan"