안녕하세요 stackoverflowers,자바 정적 생성자
방금 Java에서 프로그래밍을 시작했고 개체 인쇄와 관련하여 이상한 문제가 발생했습니다. gast 유형의 새 오브젝트가 작성되면 사용자는 자신의 생일을 입력해야합니다. 이 알은 잘 작동하지만 인쇄하려고하면 0-0-0을 반환합니다. 왜 그런가요? 그건 그렇고, 매개 변수 생성자를 사용하여 직접 새 데이텀을 만들면 올바르게 작동합니다. 어디에서 문제가 발생 했습니까? 나는 그것을 이해할 수 없다. 너희들이 나를 도울 수 있기를.
미리 감사드립니다.
public class Datum {
private static String patroon = "\\d{2}-\\d{2}-\\d{4}";
public int dag;
public int maand;
public int jaar;
Datum(int dag, int maand, int jaar) {
System.out.print("constructor: " + dag);
this.dag = dag;
System.out.println(", dag: " + this.dag);
this.maand = maand;
this.jaar = jaar;
}
Datum() {
newDatum();
}
/* */
public static Datum newDatum() {
String input = Opgave5.userInput("Geboortedatum gast");
boolean b = input.matches(patroon);
if (b) {
String[] str = input.split("-");
int dag = Integer.parseInt(str[0]);
int maand = Integer.parseInt(str[1]);
int jaar = Integer.parseInt(str[2]);
Datum datum = new Datum(dag, maand, jaar);
System.out.println(datum);
return datum;
}
else {
return new Datum();
}
}
public String toString() {
return this.dag + "-" + this.maand + "-" + this.jaar;
}
}
2 등석 :
Gast() {
this.firstName = Opgave5.userInput("Voornaam gast");
this.lastName = Opgave5.userInput("Achternaam gast");
this.geboortedatum = new Datum();
System.out.println("gast: " + this.geboortedatum); // <--- this prints out 0-0-0
}
public String toString() {
return this.firstName + " " + this.lastName + " " + this.geboortedatum;
}
감사합니다! 지금 나는 나의 실수를 이해한다 :) – user3140559