【问题标题】:Abstract class with new for Date construtor带有新的日期构造器的抽象类
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 谢谢!我现在明白了,并通过了你的代码片段。我现在看到带有 import java.util.Date 的 Date 没有数组。感谢您的帮助,先生。
【解决方案2】:

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

还有生日类属性

private Date birthdate;

【讨论】:

    【解决方案3】:

    Elliott FrischThe accepted Answer 是正确且优秀的。我没有什么要补充的。相反,我将解决两个切线问题。

    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。注意polymorphism 的作用,其中ArrayList 显示为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 () );
            }
    
        }
    
    }
    

    运行时。

    人:[Person{ name=Julie |出生日期=1954-01-07 | favoriteColor=purple }, Person{ name=Jean-Luc |出生日期=1965-02-22 |最喜欢的颜色=蓝色 }, 人物{ 姓名=丽莎 |出生日期=1977-03-18 |最喜欢的颜色=绿色 }]

    -- 年龄报告--

    朱莉:61

    让-卢克:50

    丽莎:38 岁

    【讨论】:

    • 我了解我的 Person3 类的确切目的......它为一个人收集信息。我目前对集合不感兴趣,因为我想获得继承、数组和抽象类的感觉。谢谢。
    猜你喜欢
    • 2011-11-17
    • 2020-09-14
    • 1970-01-01
    • 2018-06-10
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 2011-05-16
    相关资源
    最近更新 更多