【问题标题】:Need assistance with attributes in Java需要 Java 中的属性方面的帮助
【发布时间】:2013-02-19 00:52:02
【问题描述】:

我要做的是访问一个对象,在本例中为 date1,它有 3 个属性日、月和年。我正在尝试创建一个名为 showTomorrow() 的方法,它将以字符串格式显示对象信息 1 天。这意味着我无法更改原始对象的属性。

我已经编写了 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;
            }
    }
}

【问题讨论】:

  • 那么,每年都是闰年吗?不要滚动你自己的代码来操纵日期。
  • 你是在 date1.tomorrow() 之后调用 showDate() 吗?
  • 我完全同意@JackManey。请找一个图书馆来处理这个问题。强烈推荐Joda Time。原生 Java 时间的东西有点烂:(
  • 我需要这样做,因为这是要求我完成的工作。
  • @MarioStanicic - 然后向他们解释为什么这样做是 a) 一个糟糕的想法,b) 构建、测试和使用比使用日期操作库慢得多,以及 c) 导致错误。

标签: java string oop methods void


【解决方案1】:

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

【讨论】:

  • 问题是我一开始没有得到任何输出。我犯了错误的任何想法?
  • 另一个问题是,你一个人打电话给tomorrow()tomorrow() 不打印任何内容。尝试拨打System.out.println(date.tomorrow())
  • 我应该指出,明天()方法必须是一个字符串。我不确定这是否重要,但我收到的错误是 ./Date.java:60: 'void' type not allowed here System.out.println(showTomorrow());
  • showTomorrow() 不返回任何内容。您正在尝试打印void。请改用System.out.println(date1.tomorrow()),或仅使用date1.showTomorrow()
  • 您知道为什么它会更改我的对象属性的值吗?我只是想用明天()来展示他们后天的样子
【解决方案2】:

你是在 date1.tomorrow() 之后调用 showDate() 来显示你的输出吗?

或者代替date1.tomorrow();调用date1.showTomorrow();

【讨论】:

    【解决方案3】:

    【讨论】:

      【解决方案4】:

      您可以在 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();
      }
      

      【讨论】:

      • 这个问题是我相信他希望我们明天()也可以在该方法本身中进行显示。
      • 我添加了 public static void main 作为参考。真的吗?我不明白为什么,有一个 getDate() 函数...
      • 如果他确实希望这种情况发生,他应该添加 System.out.println(getTomorrow());
      • 目前我正在使用 void 并且出于某种原因,我的代码正在更改对象属性,而我所要做的只是显示它们前一天的内容。
      • day 属性从不递增,dayTomorrow 只递增一次,而 monthTomorrow 和 dayTomorrow 在月底相应递增。即使在多次调用 getDate 和 getTomorrow 之后,我仍然得到相同的结果。我想我没明白你的意思?
      猜你喜欢
      • 2011-04-29
      • 2013-12-20
      • 2011-10-04
      • 1970-01-01
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      • 2011-01-27
      • 2014-02-06
      相关资源
      最近更新 更多