【问题标题】:Java beginner confused about usage of constructorsJava初学者对构造函数的使用感到困惑
【发布时间】:2016-07-17 03:57:33
【问题描述】:
class Date {
    private int year;
    private String month;
    private int day;

    public Date() {
        month = "January";
        year = 1999;
        day = 1;
    }                                  //End of Constructor 1

    public Date(int year, String month, int day) {
        setDate(year, month, day);
    }                                  //End of Constructor 2

    public Date(int year) {
        setDate(year, "January", 1);
    }                                 //End of Constructor 3

    public void setDate(int year, String month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }                                //End of Constructor 4
}

public class Calendar {
    public static void main(String[] args){     
        Date date1 = new Date(2009, "March", 3);
        Date date2 = new Date(2010);
        Date date3 = new Date();
    }
}

在上面的代码中,date1、date2 和 date3 调用了哪些构造函数?调用构造函数后如何打印 date1、date2 和 date3 的结果?

我尝试了System.out.println(date1) 等等,但它给了我像u.Date@15db9742 这样的奇怪字符串。

我期待看到 2009 年 3 月 1 日或类似的时间。

【问题讨论】:

  • 1) 您不应该隐藏 Java 内置的 Date 类... 2) 您没有第四个构造函数... 这是您调用的方法... new Date().setDate(0, "test", 0)

标签: java class constructor


【解决方案1】:

当您尝试打印对象时,它的toString() 方法被调用,该方法被所有人继承 来自Object class 的java 类(默认为所有java 类的超类)。所以你必须在你的类中覆盖toString() 方法 如果您需要打印对象的某些特定内容。默认情况下,此方法打印 Class and its hash code。由于您没有覆盖toString(),因此打印的字符串 包含对象类及其哈希码(u.Date@15 ....)。 您的构造函数调用由您传递给构造函数的参数决定。 就像在date1 中一样,您按顺序传递了int,string and int 类型的3 个参数。 这与您的构造函数 2 个参数相匹配,它们是 int, string and int。 所以在你的 date1 对象构造中,构造函数 2 被调用。 类似地,对于 date2,构造函数 3 被调用 对于 date3,调用默认构造函数,即 conturcot 1。

您标记的“构造函数4”不是构造函数,它只是一个方法。 构造函数没有返回类型。

同样,要按照您在问题中的预期打印,请覆盖您的类中的 toString() 方法并在该方法中相应地格式化结果以获得预期结果。

【讨论】:

    【解决方案2】:

    date1,date2,date3 是 Date 类的对象,因此无法打印。要打印它,你必须重写 toString() 方法。

    String toString() {
    
     return year + " " + month + " " + day;
    
    }
    

    另外, 构造函数仅用于初始化字段。

    public void setDate(int year, String month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }                                //End of Constructor 4
    

    此方法不是构造函数,因为构造函数总是与类同名,并且没有明确的返回类型。

    【讨论】:

    • 能否解释一下第4个方法没有返回类型以及this.___的作用是什么?
    • 我说它不是构造函数,因为它没有类名并且返回类型为 void。
    • 构造函数用于初始化类的实例。字段是其中的一部分,但这不是构造函数的唯一功能。
    • @cricket_007:是的。
    • 没有第四种方法。示例Date 类中只有一种方法。
    【解决方案3】:

    你只需要将调用的构造函数的参数与定义的构造函数匹配,如果你没有定义任何参数,那么默认构造函数将被调用

    日期 date1 = new Date(2009, "March", 3);它会调用

     public Date(int year, String month, int day) {
            setDate(year, month, day);
        }                                  //End of Constructor 2
    

    日期 date3 = new Date();会调用

    Public Date() {
            month = "January";
            year = 1999;
            day = 1;
        }                                  //End of Constructor 1
    

    日期 date2 = new Date(2010);它会调用

    public Date(int year) {
        setDate(year, "January", 1);
    }                                 //End of Constructor 3
    

    【讨论】:

    • 您的意思是“如果您不定义任何构造函数,则将提供默认构造函数”?在 OP 的示例中,明确定义了构造函数,因此没有提供默认构造函数。
    • 是的,不能创建没有构造函数的对象。如果您没有定义任何构造函数,那么编译器将提供一个默认构造函数...!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 2013-12-31
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多