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的结果?Java初学者对构造函数的使用感到困惑
我试图System.out.println(date1)
等,但它给了我奇怪的串像[email protected]
。
我期待看到2009年3月1日或什么之类的。
1)你不应该影响内置的Java Date类... 2)你没有第四个构造函数...这是一个你调用的方法...'new Date()。setDate(0,“test”,0)' –