简体   繁体   English

Date构造函数新的抽象类

[英]Abstract class with new for Date construtor

I am new to Java and am trying to apply elements of Java that I recently read such as inheritance, arrays, and abstract classes. 我是Java新手,正在尝试应用我最近阅读的Java元素,例如继承,数组和抽象类。

I have a base class named Person3. 我有一个名为Person3的基类。 I am trying to use the Date class to get the person's birth date. 我正在尝试使用Date类获取该人的生日。

I am receiving this error: no suitable constructor found for Date(Date[]) in five places shown below with a comment. 我收到此错误:在下面显示的五个位置,没有带注释的Date(Date [])合适的构造函数。

I am researching why this error is occurring and I am not understanding. 我正在研究为什么会发生此错误,但我不理解。

Can anyone please explain this error (these errors) in detail? 任何人都可以详细解释此错误(这些错误)吗? Thank you! 谢谢!

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 doesn't have a constructor that takes an array, but you do have a birthdate field which is a Date array. Date没有采用数组的构造函数, 但是您有一个birthdate字段,它是Date数组。 I think you wanted 我想你想要

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

and

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

and

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

and

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

because new Date creates a single Date instance; 因为new Date创建了一个Date实例; not an array of Date (s). 不是Date数组。

The constructor of Person3 declares an array for thedate. Person3的构造函数为date声明一个数组。 Valid constructors for Date are ( javadoc ) 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.

Therefore change your constructor 因此更改您的构造函数

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

And the birthdate class attribute 和生日类属性

private Date birthdate;

The accepted Answer by Elliott Frisch is correct and excellent. Elliott Frisch 接受的答案是正确和出色的。 I've nothing to add there. 我没有要添加的内容。 Instead I'll address two tangents. 相反,我将解决两个切线问题。

java.time java.time

As of Java 8 and later, the new java.time framework ( Tutorial ) supplants the old java.util.Date/.Calendar classes. 从Java 8和更高版本开始,新的java.time框架( Tutorial )取代了旧的java.util.Date/.Calendar类。

I especially want to point this out to someone new to Java and just learning object-oriented programming . 我特别想向Java的新手指出这一点,他们只是学习面向对象的编程 The old date-time classes bundled with early versions of Java were a brave attempt at handling date-time, a first for the information technology industry. 与早期Java版本捆绑在一起的旧的日期时间类是在处理日期时间方面的英勇尝试,这是信息技术行业的首次尝试。 But in the end they failed. 但是最后他们失败了。 Their faults include some poor OOP design choices. 他们的缺点包括一些糟糕的OOP设计选择。 So do not look at them as good examples. 因此, 不要将它们视为很好的例子。 Better to avoid them entirely, and focus on java.time instead. 最好完全避免使用它们,而将注意力集中在java.time上。

Proper Objects 正确的对象

Your Person3 class design seems to show that you misunderstand the proper use of a class. 您的Person3类设计似乎表明您误解了对类的正确使用。 A class to represent people means each instance of that class should describe a single person. 一个代表人的类意味着该类的每个实例都应描述一个人。 Then we use collections to gather multiple Person objects as "people". 然后,我们使用集合将多个Person对象收集为“ people”。

Instead, you seem to have a spreadsheet-like arrangement in your mind. 取而代之的是,您似乎在想像电子表格那样的安排。 Looks like you are trying to have two-dimensional array listing a person in each row and attributes as columns, then shoe-horning that pseudo-spreadsheet inside an object. 看起来您正在尝试使二维数组在每一行中列出一个人,并将其属性列为一列,然后在对象内部添加该伪电子表格。 That shoe-horning does you no good as you are not take advantage of the benefits of OOP. 穿鞋拔腿对您没有好处,因为您没有利用OOP的好处。

Here is a re-worked version to demonstrate OOP design and use of java.time. 这是经过重做的版本,用于演示OOP设计和java.time的使用。

While you certainly can use plain arrays in Java (with the [] notation), we often make use of the Collections classes such as a List used here. 您当然可以在Java中使用纯数组(带有[]表示法),但我们经常使用Collections类,例如此处使用的List Notice polymorphism in action where an ArrayList is presented as a List . 注意操作中的多态性 ,其中ArrayList作为List呈现。

Note that java.time classes generally use static factory methods to instantiate objects rather than new . 请注意,java.time类通常使用静态工厂方法来实例化对象,而不是new So LocalDate.of() rater than new LocalDate() . 因此, LocalDate.of()评分器要高于new LocalDate() Also note that a time zone is required to determine a date such as "today". 另请注意,确定日期(例如“今天”)需要时区。

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

    }

}

When run. 运行时。

people: [Person{ name=Julie | 人物:[人物{name = Julie | dateOfBirth=1954-01-07 | dateOfBirth = 1954-01-07 | favoriteColor=purple }, Person{ name=Jean-Luc | 最喜欢的颜色=紫色},人物{名称=让·卢克| dateOfBirth=1965-02-22 | dateOfBirth = 1965-02-22 | favoriteColor=blue }, Person{ name=Lisa | 最喜欢的颜色=蓝色},人物{名称=莉萨| dateOfBirth=1977-03-18 | dateOfBirth = 1977-03-18 | favoriteColor=green }] 最喜欢的颜色=绿色}]

-- Age Report -- -年龄报告-

Julie : 61 朱莉:61

Jean-Luc : 50 让·卢克:50

Lisa : 38 丽莎:38

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM