无法在java中的方法中调用另一个方法

时间:2013-05-26 15:34:57

标签: java

我有这个名为Date的课程:

public class Date {

  // fields

  private int month;
  private int day;
  private int year;

  // constructor

  public Date(int month, int day, int year) {
    this.month = month;
    this.day = day;
    this.year = year;
  }

  //check for leap year or not
  public static boolean isLeap(int year) {
    if (year % 4 != 0) {
      return false;
    } else if (year % 400 == 0) {
      return true;
    } else if (year % 100 == 0) {
      return false;
    } else {
      return true;
    }

  }

  public static boolean isValid(int month, int day, int year) {
    if (year < 1900) {
      return false;
    } else {
      if (month > 0 && month < 13) {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
          return day > 0 && day <= 31;

        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
          return day > 0 && day <= 30;
        } else if (month == 2) {
          if (isLeap(year)) {
            return day > 0 && day <= 29;
          } else {
            return day > 0 && day <= 28;
          }
        }

      } else {
        return false;
      }
    }
    return true;
  }

  public int getMonth() {
    return month
  }

  public void setMonth(int month) {
    this.month = month;
  }

  public int getDay() {
    return day;
  }

  public void setDay(int day) {
    this.day = day;
  }

  public int getYear() {
    return year;
  }

  public void setYear(int year) {
    this.year = year;
  }

  public void setDate(int month, int day, int year) {
    if (isValid(month, day, year)) {
      setDay(day);
      setMonth(month);
      setYear(year);
    } else {
      setDay(1);
      setMonth(1);
      setYear(1900);
    }
  }
//

  public String displayDate() {
    return month + "/" + day + "/" + year;
  }

  public String add(int n) {
    if (n <= 20) {
      if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
        if ((day + n > 0) && (day + n <= 31))
          day = day + n;
        else
          month += 1;
        if (month > 12)
          year += 1;
      }
      if (month == 4 || month == 6 || month == 9 || month == 11) {
        if ((day + n > 0) && (day + n <= 30))
          day = day + n;
        else
          month += 1;
        if (month > 12)
          year += 1;
      }
    }
    return month + "/" + day + "/" + year;
  }

  public String sub(int n) {
    if (n <= 20) {
      if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        if (day - n < 0)
          day = 31 + day - n;
        else
          month -= 1;
      if (month < 0)
        year -= 1;
      if (month == 4 || month == 6 || month == 9 || month == 11)
        if ((day - n < 0))
          day = 30 + day - n;
        else
          month -= 1;
      if (month < 0)
        year -= 1;

    }
    return month + "/" + day + "/" + year;
  }

}

这个类来测试Date类:

public class MyDateTester {
    public static void main(String[] args) {
        new MyDateTester();
    }

    Date theClass;

    public MyDateTester(){
        theClass = new Date(6,1,1600);
        System.out.print("Month is: ");
        System.out.println(theClass.getMonth());
        System.out.print("Day is: ");
        System.out.println(theClass.getDay());
        System.out.print("Year is: ");
        System.out.println(theClass.getYear());
        System.out.println(theClass.displayDate());
        System.out.println(theClass.add(20));
        System.out.println(theClass.isValid(6,1,2000));
    }
}

当我将6,1,1600值赋予Date时,仍然打印6,1,1600,setDate方法中的isValid方法未设置日期。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

在测试中,您正在使用其构造函数创建日期。您的构造函数不会调用setDate()isValid().

 public Date(int month, int day, int year){
    this.month = month;
    this.day = day;
    this.year = year;
 }

您可以通过在Date构造函数中调用setDate()来轻松解决此问题。

 public Date(int month, int day, int year){
    setDate( month, day, year );
 }

答案 1 :(得分:0)

你必须在你的构造函数中为Date

调用你的setDate()方法