gpt4 book ai didi

java - 在 Java 中使用 Student 类中的 Address 类和 toString

转载 作者:行者123 更新时间:2023-12-02 08:57:11 26 4
gpt4 key购买 nike

我正在尝试创建一个程序,您可以在其中添加学生信息及其地址。因此,我创建了 2 个类,Student 类和 Adres 类(荷兰语中的地址)。在 Adres 类中,我有地址数据,在 Student 类中有学生数据。

但是我在为我的 Student 类创建构造函数时遇到了困难,因为当您添加新学生时,还需要添加该学生的地址。

我尝试在 Student 中使用 toString 方法,但也需要返回地址。

所以我有 3 个问题:

  1. 如何为 Student 类设置构造函数,其中还需要添加地址
  2. 如何设置 toString 方法,该方法还从 Adres 返回“toString”。
  3. 添加新学生时如何输入本地日期和地址。 (本地日期用于学生生日)

我对 java 还很陌生,如果有人能帮助我那就太好了。

我的地址类:

package oop3.studenten;

public class Adres {

private String straat;
private Integer huisnr;
private String postcode;
private String plaats;

public Adres(String straat, Integer huisnr, String postcode, String plaats) {
this.straat = straat;
this.huisnr = huisnr;
this.postcode = postcode;
this.plaats = plaats;
}


public String toString() {
return straat + "" + huisnr + "," + postcode + "" + plaats;
}




// Using regex to check if postcode is valid
public static boolean checkPostcode(String postCode) {
return postCode.matches("[1-9][0-9]{3}[a-zA-Z]{2}");
}


}

我的学生类(class):

package oop3.studenten;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Student {

private Integer studentnr; //StudentId
private String voornaam; //Firstname
private String achternaam; //Lastname
private LocalDate geboortedatum; //Birthday
private Adres adres; //address

// constructor for Student
public Student(Integer studentnr, String voornaam, String achternaam, LocalDate geboortedatum, Adres adres){
this.studentnr = studentnr;
this.voornaam = voornaam;
this.achternaam = achternaam;
this.geboortedatum = geboortedatum;
this.adres = new Adres();
}

// toString method for Student
@Override
public String toString() {
return "Student{" +
"studentnr=" + studentnr +
", voornaam='" + voornaam + '\'' +
", achternaam='" + achternaam + '\'' +
", geboortedatum=" + korteGeboortedatum(geboortedatum) +
", adres=" + adres +
'}';
}

// method to return birthday (geboortedatum) in day month year format
public String korteGeboortedatum(LocalDate gebdatum ){
return gebdatum.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
}

}

以及我尝试添加学生的主类(class)

package oop3.studenten;


public class Main {

public static void main(String[] args) {

Student student = new Student(500739074, "Ronny", "Giezen", 22111997, "?"
);
}

}

提前致谢

最佳答案

学生构造函数:

public Student(Integer studentnr, String voornaam, String achternaam, LocalDate geboortedatum, Adres adres){
this.studentnr = studentnr;
this.voornaam = voornaam;
this.achternaam = achternaam;
this.geboortedatum = geboortedatum;
this.adres = adres; //if you do "new Adres();" a completely empty adres instance would be created, instead you want to use the one passed as parameter
}

// toString method for Student
@Override
public String toString() {
return "Student{" +
"studentnr=" + studentnr +
", voornaam='" + voornaam + '\'' +
", achternaam='" + achternaam + '\'' +
", geboortedatum=" + korteGeboortedatum(geboortedatum) +
", adres=" + adres.toString() +
'}'; // ^^ calling the .toString()-method of adres and appending it to the rest
}

主类:

public static void main(String[] args) {
Adres adres = new Adres("Mainstreet", 5, "48484", "Amsterdam"); //creating the adress
LocalDate birthday = LocalDate.of(2017, 1, 13); //creating the birthday-localdate
Student student = new Student(500739074, "Ronny", "Giezen", birthday, adres); //passing the birthday & adres to the student-constructor
}

关于java - 在 Java 中使用 Student 类中的 Address 类和 toString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60423518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com