Java 具有新的日期构造函数的抽象类
我是Java新手,正在尝试应用我最近读到的Java元素,如继承、数组和抽象类 我有一个名为Person3的基类。我正在尝试使用Date类获取此人的出生日期 我收到这个错误:在下面显示的五个地方找不到适合Date(Date[])的构造函数,并附上注释 我正在研究为什么会发生这种错误,我不明白 有人能详细解释一下这个错误吗?谢谢大家!Java 具有新的日期构造函数的抽象类,java,inheritance,abstract-class,Java,Inheritance,Abstract Class,我是Java新手,正在尝试应用我最近读到的Java元素,如继承、数组和抽象类 我有一个名为Person3的基类。我正在尝试使用Date类获取此人的出生日期 我收到这个错误:在下面显示的五个地方找不到适合Date(Date[])的构造函数,并附上注释 我正在研究为什么会发生这种错误,我不明白 有人能详细解释一下这个错误吗?谢谢大家! public abstract class Person3 extends Object{ private String [] name; priv
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);
}
}
}
公共抽象类Person3扩展对象{
私有字符串[]名称;
私人日期[]出生日期;
私人和社会;
公职人员3()
{
for(int i=0;i
Date
没有接受数组的构造函数,但是您确实有一个生日
字段,它是一个日期
数组。我想你想要
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 = theDate;
social = theSocial;
}
及
及
及
因为
newdate
创建了一个Date
实例;不是日期的数组。Person3的构造函数声明日期的数组。日期的有效构造函数为()
因此,请更改构造函数
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 = theDate;
social = theSocial;
}
和birthdate类属性
private Date birthdate;
这是正确和优秀的。我没有什么要补充的。相反,我将处理两个切线
java.time
从Java8及更高版本开始,新框架()取代了旧的Java.util.Date/.Calendar类
我特别想向刚接触Java并正在学习的人指出这一点。与Java早期版本捆绑在一起的旧日期时间类是处理日期时间的一次大胆尝试,这在信息技术行业尚属首次。但最终他们失败了。他们的缺点包括一些糟糕的OOP设计选择。因此,不要将它们视为好的例子。最好完全避免使用它们,而将重点放在java.time上
适当对象
您的人员3
类设计似乎表明您误解了类的正确使用。表示人的类意味着该类的每个实例都应该描述一个人。然后我们使用集合将多个Person
对象收集为“people”
相反,你的脑海中似乎有一个类似电子表格的安排。看起来您正在尝试让二维数组在每行中列出一个人,并将属性列为列,然后在对象中添加伪电子表格。这种鞋角对你没有好处,因为你没有利用OOP的好处
这里是一个重新设计的版本,演示了面向对象的设计和java.time的使用
虽然您当然可以在Java中使用普通数组(使用[]
符号),但我们经常使用这里使用的类,例如。请注意,在操作中,将显示为列表
请注意,java.time类通常使用静态工厂方法来实例化对象,而不是new
。因此,LocalDate.of()
比new LocalDate()
评级更高。还要注意,需要时区来确定日期,例如“今天”
package timestaff;
导入java.time.LocalDate;
导入java.time.Period;
导入java.time.ZoneId;
导入java.time.ZoneOffset;
导入java.util.ArrayList;
导入java.util.List;
公共阶层人士{
私有字符串名称;
私有本地出生日期;
私有字符串偏爱颜色;
公共人物(字符串名称、LocalDate出生日期、字符串favoriteColor){
this.name=名称;
this.dateOfBirth=出生日期;
this.favoriteColor=favoriteColor;
}
公开发售(){
ZoneId ZoneId=ZoneOffset.UTC;
LocalDate today=LocalDate.now(zoneId);
期间年龄=期间之间(this.dateOfBirth,today);
整数年=age.getYears();
回归年;
}
public void setBirthDate(Date [] newDate)
{
if (newDate == null)
{
System.out.println("Fatal Error setting person birthdate.");
System.exit(0);
}
else
birthDate = newDate;
}
public Date(int year, int month, int day)
//Deprecated.
//instead use the constructor Date(long date)
public Date(long date)
//Constructs a Date object using the given milliseconds time value.
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 = theDate;
social = theSocial;
}
private Date birthdate;
package timestuff;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
public class Person {
private String name;
private LocalDate dateOfBirth;
private String favoriteColor;
public Person ( String name , LocalDate dateOfBirth , String favoriteColor ) {
this.name = name;
this.dateOfBirth = dateOfBirth;
this.favoriteColor = favoriteColor;
}
public Integer yearsOld () {
ZoneId zoneId = ZoneOffset.UTC;
LocalDate today = LocalDate.now ( zoneId );
Period age = Period.between ( this.dateOfBirth , today );
Integer years = age.getYears ();
return years;
}
@Override
public String toString () {
return "Person{ " + "name=" + name + " | dateOfBirth=" + dateOfBirth + " | favoriteColor=" + favoriteColor + " }";
}
public static void main ( String[] args ) {
Person julie = new Person ( "Julie" , LocalDate.of ( 1954 , 1 , 7 ) , "purple" );
Person jeanluc = new Person ( "Jean-Luc" , LocalDate.of ( 1965 , 2 , 22 ) , "blue" );
Person lisa = new Person ( "Lisa" , LocalDate.of ( 1977 , 3 , 18 ) , "green" );
List<Person> people = new ArrayList<> ();
people.add ( julie );
people.add ( jeanluc );
people.add ( lisa );
System.out.println ( "people: " + people );
System.out.println ( "" ); // blank line.
System.out.println ( "-- Age Report --" );
for ( Person person : people ) {
System.out.println ( person.name + " : " + person.yearsOld () );
}
}
}