java - 比较两个对象,并返回字符串。但一个对象不带参数?

标签 java class object

这是家庭作业。

目标:我想比较两个对象的日期来确定我的人物对象是否是成人,并将其存储在字符串中。

奇怪的是,我的日期d1的值都是0;

public class Date {

  public int day, month, year;  
  public String child

  Date(date d1, date d2) {
      if ((d1.year - d2.year > 18) ||
          ((d1.year  - d2.year == 18) && (d2.year> d1.year)) ||
          ((d1.year  - d2.year == 18) && (d2.year == d1.maand) && (d2.day > d1.day))) {
             child = adult;  
      } else {
            child = child;
  }

  Date(int a, int b, int c) {
    a = year;
    b = month; 
    c = day; 
  }

  Date (String birthdate) {
    String pattern = "\\d{2}-\\d{2}-\\d{4}";
    boolean b = birthdate.matches(pattern);
    if (b) {
        String[] str = birthdate.split("-"); 
        for (String s: str)
            this.day = Integer.parseInt(str[0]);
            this.month = Integer.parseInt(str[1]);
            this.year = Integer.parseInt(str[2]);
            this.child = false; 
    } else {
          System.out.println("Wrong format");
    } 

}

当我进行测试时,会发生这种情况:

  System.out.println("D1 year = " + d1.year); 
  System.out.println("D1 day = " + d1.day); 
  System.out.println("D1 month = " + d1.month);
Result: 
D1 year = 0
D1 day = 0
D1 month = 0

为什么会发生这种情况?让我们看看我的另一个类。

我的另一个类,我的方法 infoPerson 所在的位置如下:

    public static Person infoPerson() {

       String name, lastname, birthdate;  
       Datum birthday, today;  

       System.out.println("Firstname:");
       name = userInput();  
       System.out.println("Lastname:");
       lastname = userInput(); 
       System.out.println("Birthdate?:");
       birthdate = userInput(); 

       //here I send the string birthdate to my Date class
       birthday = new Date(birthdate); 
       today = new Date(3, 7, 2013); 


      //Here I want to compare my two Date objects, today and birthday. This is were I got stuck, how do I do this correctly?
      dateChild = new Date(today, birthday); 

      // here i send the new date to my Person class what consists of two strings and Data birthday
      return new Gast(name, lastname, dateChild); 

   }

最佳答案

构造函数中的赋值相反:

Date(int a, int b, int c) {
    a = year;    // should be year = a;
    b = month;   // month = b;
    c = day;     // day = c;
}

请不要使用与Java API中定义的类名相同的类名。 Date 已经是 java.util 包中的一个类。

除此之外,您的代码中还有许多编译器错误:

  • public string child - 无法编译。应该是字符串而不是字符串
  • voidcompareTo(date d1, date d2) - 我不知道你想在这里做什么。但这也无法编译。未定义类型 - 日期
  • 您已声明Datumbirthday并使用newDate(...)对其进行初始化。那也行不通。
  • 出于某种原因,我觉得你的类中没有任何方法,而只有一堆构造函数。我的建议是 - 扔掉该代码,然后重新开始。
  • 并且请不要使用一堆整数字段来存储生日。使用Calendar而是实例。

关于java - 比较两个对象,并返回字符串。但一个对象不带参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19212116/

相关文章:

JavaScript "classes"

javascript - JavaScript 中的 PHP 变量属性

JavaScript 随机报价生成器

java - 查找另一个元素内的所有 anchor 元素的 xpath 是什么?

java - 是否可以像在 gradle 中设置 sonar.sources 一样在 jenkins 中设置?

java - 如何为嵌入式 jetty/ Spring 安全启用 HTTP 摘要?

ios - 使用 Class/Struct in collection Cell inside collection Cell for future pass data

c++ - new MyClass 和 new MyClass() 有什么区别

swift - 如何在 swift 中使用带有开关的 AS 来获取类类型

java - Android 游戏生成激光 Sprite