【发布时间】:2015-10-18 19:27:15
【问题描述】:
我是 Java 新手,正在尝试应用我最近阅读的 Java 元素,例如继承、数组和抽象类。
我有一个名为 Person3 的基类。我正在尝试使用 Date 类来获取此人的出生日期。
我收到此错误:在下面显示的五个位置中找不到适合 Date(Date[]) 的构造函数并带有注释。
我正在研究为什么会发生此错误,但我不明白。
谁能详细解释这个错误(这些错误)?谢谢!
public abstract class Person3 extends Object{
private String [] name;
private Date [] birthdate;
private int [] social;
public Person3()
{
for(int i = 0; i < name.length; i++) {
System.out.println("INSIDE");
name[i] = "No name";
}
// birthdate = new Date("Jan", 1, 1000);
for(int i = 0; i < birthdate.length; i++){
birthdate[i]= new Date ("Jan", 1, 1000);
}
//social = 00000000;
for(int i = 0; i < social.length; i++) {
social[i] = 00000000;
}
}
/**
Precondition: Neither theName nor theDate is null.
*/
public Person3(String [] theName, Date [] theDate, int [] theSocial)
{
if (theName == null || theDate == null || theSocial == null)
{
System.out.println("Fatal Error creating employee.");
System.exit(0);
}
name = theName;
birthdate = new Date(theDate); //ERROR
social = theSocial;
}
public Person3(Person3 originalObject)
{
name = originalObject.name;
birthdate = new Date(originalObject.birthdate); //ERROR
social = originalObject.social;
}
abstract double getPay( );
public String [] getName( )
{
return name;
}
public Date [] getbirthDate( )
{
return new Date(birthdate); //ERROR
}
public int [] getSocial(){
return social;
}
/**
Precondition newName is not null.
*/
public void setName(String [] newName)
{
if (newName == null)
{
System.out.println("Fatal Error setting employee name.");
System.exit(0);
}
else
name = newName;
}
/**
Precondition newDate is not null.
*/
public void setBirthDate(Date [] newDate)
{
if (newDate == null)
{
System.out.println("Fatal Error setting person birthdate.");
System.exit(0);
}
else
birthDate = new Date(newDate); //ERROR
}
public void setSocial(int [] newSocial){
if(newSocial == null){
System.out.println("Fatal Error setting person social.");
System.exit(0);
}
}
}
【问题讨论】:
-
你能分享一下 Date 类吗?
-
@Siddhartha 很确定他正在使用
java.util.Date。所以在那张纸条上,没有构造函数可以接受 Date[] -
为什么
birthdate是一个日期数组?你的人有多少个生日? -
@Siddhartha 我正在使用 java.util.Date
-
@Zapl 我正在尝试了解数组在 Java 中的作用。我想要一个生日数组,这样我的 Student、Staff 和 Faculty 子类就可以添加元素(生日)。
标签: java inheritance abstract-class