抽象 class 与新的日期构造函数

Abstract class with new for Date construtor

我是 Java 的新手,正在尝试应用我最近阅读的 Java 的元素,例如继承、数组和抽象 classes。

我有一个名为 Person3 的基础 class。我正在尝试使用日期 class 来获取此人的出生日期。

我收到此错误:在下面显示的五个位置找不到适合 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 没有接受数组的构造函数,但是 你有一个 birthdate 字段,它是一个 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;
}

public Person3(Person3 originalObject)
{
     name = originalObject.name;
     birthdate = originalObject.birthdate;
     social = originalObject.social;
}

public Date [] getbirthDate( )
{
    return birthdate;
}

public void setBirthDate(Date [] newDate)
{
    if (newDate == null)
    {
         System.out.println("Fatal Error setting person birthdate.");
         System.exit(0);
    }
    else
        birthDate = newDate;
}

因为 new Date 创建了一个 Date 实例;不是 Date(s).

的数组

Person3 的构造函数为日期声明了一个数组。 Date 的有效构造函数是 (javadoc)

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;
}

和生日 class 属性

private Date birthdate;

by Elliott Frisch正确,优秀。我没有什么可补充的。相反,我将解决两个切线问题。

java.time

从 Java 8 开始,新的 java.time framework (Tutorial) 取代旧的 java.util.Date/.Calendar classes.

我特别想向刚接触 Java 并且刚刚学习 object-oriented programming 的人指出这一点。与 Java 的早期版本捆绑在一起的旧日期时间 classes 是处理日期时间的勇敢尝试,这在信息技术行业尚属首次。但最终他们失败了。他们的错误包括一些糟糕的 OOP 设计选择。因此,不要 将它们视为很好的例子。最好完全避免它们,而是专注于 java.time。

适当的对象

您的 Person3 class 设计似乎表明您误解了 class 的正确用法。 class 代表人意味着 class 的每个实例都应该描述一个人。然后我们使用集合将多个Person对象收集为“人”。

相反,您的脑海中似乎有一个类似电子表格的安排。看起来您正在尝试让二维数组在每一行中列出一个人并将属性列为列,然后将伪电子表格插入一个对象中。擦鞋对你没有好处,因为你没有利用 OOP 的好处。

这是一个重新设计的版本,用于演示 java.time 的 OOP 设计和使用。

虽然您当然可以在 Java 中使用普通数组(使用 [] 符号),但我们经常使用 Collections classes such as a List used here. Notice polymorphism in action where an ArrayList 表示为 List

注意java.time classes一般使用静态工厂方法实例化对象而不是new。所以 LocalDate.of() 的评分高于 new LocalDate()。另请注意,需要时区才能确定日期,例如“今天”。

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 () );
        }

    }

}

当运行.

people: [Person{ name=Julie | dateOfBirth=1954-01-07 | favoriteColor=purple }, Person{ name=Jean-Luc | dateOfBirth=1965-02-22 | favoriteColor=blue }, Person{ name=Lisa | dateOfBirth=1977-03-18 | favoriteColor=green }]

-- Age Report --

Julie : 61

Jean-Luc : 50

Lisa : 38