【发布时间】: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