gpt4 book ai didi

java - 带参数的对象仍显示引用变量的初始值

转载 作者:行者123 更新时间:2023-11-30 05:58:24 25 4
gpt4 key购买 nike

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。你怎么认为 ?顺便说一句,这是我在这里发表的第一篇文章,如果这不是在这里发布的正确形式,我很抱歉。谢谢!

最佳答案

您的构造函数应该是:

public Date(int theDay, int theMonth, int theYear) {

this.day = theDay;
this.month = theMonth;
this.year = theYear;
}

基本上,您需要为传递给实例变量的值赋值。

关于java - 带参数的对象仍显示引用变量的初始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52738644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com