需要Java中的属性帮助

时间:2013-03-04 19:52:41

标签: java string oop methods void

我要做的是访问一个对象,在这种情况下,date1具有3个属性日,月和年。我正在尝试创建一个名为showTomorrow()的方法,该方法将以字符串格式显示对象信息。这意味着我无法改变原始对象的属性。

我已经编写了Data.java程序,如果有人能指出我正确的方向或向我展示它真正有用的话,它会在下面显示。

这就是我基本上在我认为的主要方法上运行的。

**Date date1 = new Date(30, 12, 2013)** // instantiate a new object with those paramaters

**date1.showDate();** // display the original date

**date1.tomorrow();** // shows what that date would be 1 day infront

问题是现在它没有显示任何东西。我以为通过说dayTomorrow = this.day ++;我将它的默认值+ 1天添加到变量dayTomorrow。

public class Date
{
    private int day;
    private int month;
    private int year;
    private int dayTomorrow;
    private int monthTomorrow;
    private int yearTomorrow;

    public Date()
    {
            day = 1;
            month = 1;
            year = 1970;
    }
    public Date(int inDay, int inMonth, int inYear)
    {
            day = inDay;
            month = inMonth;
            year = inYear;
    }
    public void setDate(int inDay, int inMonth, int inYear)
    {
            day = inDay;
            month = inMonth;
            year = inYear;
    }
    public String getDate()
    {
            String strDate;
            strDate = day + "/" + month + "/" + year;
            return strDate;
    }
    public String getTomorrow()
    {
            String strTomorrow;
            strTomorrow = dayTomorrow + "/" + monthTomorrow + "/" + yearTomorrow;
            return strTomorrow;
    }
    public String tomorrow()
    {
            dayTomorrow = this.day++;
            monthTomorrow = this.month;
            yearTomorrow = this.year;

            if(dayTomorrow > 30)
            {
                    dayTomorrow = 1;
                    monthTomorrow = this.month++;
            }
            if(monthTomorrow > 12)
            {
                    monthTomorrow = 1;
                    yearTomorrow = this.year++;
            }

            return getTomorrow();
    }
    public void showDate()
    {
            System.out.print("\n\n THIS OBJECT IS STORING ");
            System.out.print(getDate());
            System.out.print("\n\n");
    }
    public void showTomorrow()
    {
            System.out.print("\n\n THE DATE TOMORROW IS ");
            System.out.print(getTomorrow());
            System.out.print("\n\n");
    }
    public boolean equals(Date inDate)
    {
            if(this.day == inDate.day && this.month == inDate.month && this.year == inDate.year)
            {
                    return true;
            }
            else
            {
                    return false;
            }
    }
}

4 个答案:

答案 0 :(得分:2)

您只需使用++this.day++this.month++this.year即可。使用this.day++时,它会返回上一个日期值,而不是新值。将++放在前面可以解决问题。此外,它会更改day值...您可能希望将其更改为this.day + 1

答案 1 :(得分:0)

您是否在date1.tomorrow()之后调用showDate()来显示输出?

或代替date1.tomorrow();致电date1.showTomorrow();

答案 2 :(得分:0)

答案 3 :(得分:0)

您可以在java中使用本机日期支持,但我认为您只是在练习吗?

这应该可以解决问题:

public class Date {
    private int day = 1;
    private int month = 1;
    private int year = 1970;
    private int dayTomorrow = day+1;
    private int monthTomorrow;
    private int yearTomorrow;

    public Date()
    {
            tomorrow();
    }
    public Date(int inDay, int inMonth, int inYear)
    {
            day = inDay;
            month = inMonth;
            year = inYear;
            tomorrow();
    }
    public void setDate(int inDay, int inMonth, int inYear)
    {
            day = inDay;
            month = inMonth;
            year = inYear;
    }
    public String getDate()
    {
            String strDate;
            strDate = day + "/" + month + "/" + year;
            return strDate;
    }
    public String getTomorrow()
    {
            String strTomorrow;
            strTomorrow = dayTomorrow + "/" + monthTomorrow + "/" + yearTomorrow;
            return strTomorrow;
    }
    public void tomorrow()
    {
            monthTomorrow = this.month;
            yearTomorrow = this.year;

            if(dayTomorrow > 30)
            {
                    dayTomorrow = 1;
                    monthTomorrow = this.month++;
            }
            if(monthTomorrow > 12)
            {
                    monthTomorrow = 1;
                    yearTomorrow = this.year++;
            }
    }
    public void showDate()
    {
            System.out.print("\n\n THIS OBJECT IS STORING ");
            System.out.print(getDate());
            System.out.print("\n\n");
    }
    public void showTomorrow()
    {
            System.out.print("\n\n THE DATE TOMORROW IS ");
            System.out.print(getTomorrow());
            System.out.print("\n\n");
    }
    public boolean equals(Date inDate)
    {
            if(this.day == inDate.day && this.month == inDate.month && this.year == inDate.year)
            {
                    return true;
            }
            else
            {
                    return false;
            }
    }
}

仔细查看我所做的任何更改;) 这是主要的:

public static void main(String[] args) {
    Date d = new Date();
    d.showDate();
    d.showTomorrow();
}