public class Date {
private int day;
private int month;
private int year;
public Date(int theDay, int theMonth, int theYear) {
}
public void setDay(int theDay) {
day = theDay;
}
public int getDay() {
return day;
}
public void setMonth(int theMonth) {
month = theMonth;
}
public int getMonth() {
return month;
}
public void setYear(int theYear) {
year = theYear;
}
public int getYear() {
return year;
}
public void displayDate() {
System.out.printf("The current date is: %d/%d/%d", getDay(), getMonth(), getYear() );
}
}
+
public class DateTest {
public static void main( String[] args ) {
Date myDate = new Date(20, 5, 2010);
myDate.displayDate();
}
}
结果:当前日期是:0/0/0 预期结果:2010年5月20日
我检查了很多次,看不到任何错误。确保已记录更改并重新启动Eclipse。你怎么看 ?顺便说一句,这是我在这里的第一篇文章,如果这不是在此发布的正确形式,那么抱歉。 谢谢!
答案 0 :(得分:1)
您的构造函数应为:
public Date(int theDay, int theMonth, int theYear) {
this.day = theDay;
this.month = theMonth;
this.year = theYear;
}
基本上,您需要分配要传递给实例变量的值。
答案 1 :(得分:0)
在构造函数中,您正在争论“ theDay”,“ theMonth”,“ theYear”。将它们设置为等于“天”,“月”,“年”类变量。
day=theDay
month=theMonth
year=theYear
内部构造函数
答案 2 :(得分:0)
您的构造函数对您的字段不执行任何操作
public Date(int theDay, int theMonth, int theYear) {
}
在创建对象时初始化文件:
public Date(int theDay, int theMonth, int theYear) {
day=theDay;
month=theMonth;
year=theYear;
}