Date for Construtor

时间:2015-10-18 19:27:15

标签: java inheritance abstract-class

我是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);
        }
    }
}

3 个答案:

答案 0 :(得分:3)

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)的数组。

答案 1 :(得分:1)

Person3的构造函数为thedate声明一个数组。 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;
}

和birthdate类属性

private Date birthdate;

答案 2 :(得分:1)

The accepted AnswerElliott Frisch是正确且优秀的。我没有什么可以补充的。相反,我将解决两个切线问题。

java.time

从Java 8及更高版本开始,新的java.time框架(Tutorial)取代了旧的java.util.Date/.Calendar类。

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

正确的对象

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

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

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

虽然您当然可以在Java中使用普通数组(使用[]表示法),但我们经常使用Collections类,例如此处使用的List。请注意polymorphismArrayList呈现为List的情况下的行动。

请注意,java.time类通常使用静态工厂方法来实例化对象而不是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 () );
        }

    }

}

跑步时。

  

人:[人{姓名=朱莉| 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}]

     

- 年龄报告 -

     朱莉:61

     

Jean-Luc:50

     

丽莎:38