java - 我想编辑我的类的第四个变量,但我似乎将它们全部编辑

标签 java class oop

我的程序现在做什么:

  • 我使用以下信息创建一个人员对象:firstnamelastnamebirthdate(数据是不同的类)。
  • 日期类有四个变量:日、月、年和 18+(是或否)。

什么有效:我可以成功创建一个具有名字姓氏出生日期的人物对象。

我的人员类(class)(看起来有效)。

public class Person {

    public String firstName;
    public String lastName;
    public Date date; 

    public String toString() {
        return (firstName + " " + lastName + " (" + date); 
    }


    public Person(String firstName, String lastName, Date date) {
        this.firstName = firstName; 
        this.lastName = lastName; 
        this.date = date; 
    }

}

我的类(class)包括我的主要类(class),其中我还有一个创建我的人的方法。

public static Person setName() {

    String name; 
    String lastName
    String inputBirthdate;
    Date niceDate 
    Date newDate; 


    System.out.println("Firstname:");
    firstName = userInput();  
    System.out.println("Lastname:");
    lastName = userInput(); 
    System.out.println("Birthday:");
    inputBirthdate = userInput(); 
    niceDate = new Date(inputBirthdate);
    newDate = new Date(niceDate);

    return new Gast(firstName, lastName, newDate); 

}

然后我有我的 Date 类,我在其中检查日期的输入是否正确。请注意,我可以让我的 Date 类在没有第四个变量的情况下正常工作。

public class Date {

public String day; 
public String month; 
public String year;
public boolean child; 

public String toString() {
    return (day + "." + month + "." + year + "." + child);
}


/*Date(String day, String month, String year, boolean child) {
    this.day = dag; 
    this.month = month; 
    this.year = year; 
    this.child = child; 
}*/ //don't need this one, output is the same

public Date(Datum niceDate) {
    int bYear = Integer.parseInt(niceDate.year;
    int bMonth = Integer.parseInt(niceDate.day);
    int bDay = Integer.parseInt(niceDate.day);

    boolean child = false; 

    if (bYear > 1995) {
        this.child= true; 
    } else if (bYear == 1995 && bMonth > 10) {
        this.child = true; 
    } else if (bYear == 1995 && bMonth == 10 && bDay > 1) {
        this.child = true; 
    } else {
        this.child = false; 
    }  

}

public Date(String birthdate) {
    String patroon = "\\d{2}-\\d{2}-\\d{4}";
    boolean b = birthdate.matches(patroon);
    if (b) {
        String[] str = birthdate.split("-"); 
        for (String s: str)
        this.day = str[0];
        this.month = str[1];
        this.year = str[2];
    }
    else {
        System.out.println("Birthday is formatted wrong");
    } 
}

}

如果我运行这个(无论是否检查成人(检查看起来有效!),但是,我输入的 birthdate 返回 null:

   Room 1: Name name (null.null.null)false    //boolean works, date not
   Room 2: available

我认为问题在于,在我的 Date 类中的第二个方法中,public Date(Date Nicedate) 在将我的日期解析为 int 后将其删除。

所以基本上我只想返回 boolean 值并保持我的字符串完全相同,并且只编辑它们以将它们用作 Int 进行计算。

有人能指出我正确的方向吗?也许这是一个非常简单的解决方案,但我已经研究了一整天,但没有看到解决方案。

按要求编辑:(我在公共(public)日期(datum NiceDate)中有此声明,但日期仍然不会显示。嗯嗯:

public Date(Datum niceDate) {
        this.year = year; 
        this.day = day; 
        this.month = month; 

    int bYear = Integer.parseInt(niceDate.year;
    int bMonth = Integer.parseInt(niceDate.day);
    int bDay = Integer.parseInt(niceDate.day);

    boolean child = false; 

    if (bYear > 1995) {
        this.child= true; 
    } else if (bYear == 1995 && bMonth > 10) {
        this.child = true; 
    } else if (bYear == 1995 && bMonth == 10 && bDay > 1) {
        this.child = true; 
    } else {
        this.child = false; 
    }  

}

public Date(String birthdate) {
    String patroon = "\\d{2}-\\d{2}-\\d{4}";
    boolean b = birthdate.matches(patroon);
    if (b) {
        String[] str = birthdate.split("-"); 
        for (String s: str)
        this.day = str[0];
        this.month = str[1];
        this.year = str[2];
    }
    else {
        System.out.println("Birthday is formatted wrong");
    } 
}

}

最佳答案

问题是,当您从 niceDate 创建 newDate 时,您并没有复制日/月/年。

如果您查看 public Date(Datum NiceDate) 构造函数,您设置的唯一实例变量是 this.child,但您还需要设置 this.daythis.monththis.year

此外,我建议您创建一个名为 isAdult 的日期计算函数作为 Date 类的方法,然后只需调用 niceDate.isAdult( ) 如果您需要显示日期是否是 18 年前。否则,很容易出错,导致 this.child 不正确。

关于java - 我想编辑我的类的第四个变量,但我似乎将它们全部编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19201017/

相关文章:

java - ConcurrentMap 不一致

java - 传递空整数作为参数

java - 使用静态变量调用对象

c++ - 定义类中声明的变量的范围(C++)?

java - 是否有任何 Java 包结构模式可以证明默认可见性修饰符的存在?

java - JUnit 测试挂起

Eclipse中Tomcat下的Java ClassNotFoundException

c++ - 将基类添加到 "New C++ Class"对话框

java - 使用一系列 lambda 函数来定义类功能是否合适?

C# 面向对象的子调用返回类型 "this"