将Student类中的Adress类与java中的toString一起使用

问题描述 投票:0回答:1

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

但是我被困在为我的学生类创建一个构造函数上,因为当您添加一个新学生时,还需要添加该学生的地址。

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

所以我有3个问题:

  1. 如何在还需要添加地址的Student类中设置构造函数
  2. 如何设置toString方法,它也从Adres返回“ toString”。
  3. 添加新学生时,我如何输入LocalDate和地址。 (localdate用于学生生日)

我对Java还是很陌生,如果有人可以帮助我,那就太好了。

我的Adres班级:

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}");
    }


}

我的学生班:

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"));
        }

}

以及我试图增加学生的主要班级

package oop3.studenten;


public class Main {

    public static void main(String[] args) {

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

}

提前感谢

java constructor tostring
1个回答
1
投票

学生构造函数:

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
}

主类:

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
}

0
投票

在主类中,您可以先创建地址对象,然后再创建学生。在学生构造函数中,您将可以传递地址对象。

public static void main(String[] args) {
        Address studentAddress = new Address("straat", 1, "postcode", "plaats")
        Student student = new Student(500739074, "Ronny", "Giezen", null, studentAddress);
    }
  1. 问题您只需调用在您的地址类别中创建的toString()-方法
@Override
    public String toString() {
        return "Student{" +
                "studentnr=" + studentnr +
                ", voornaam='" + voornaam + '\'' +
                ", achternaam='" + achternaam + '\'' +
                ", geboortedatum=" + korteGeboortedatum(geboortedatum) +
                ", adres=" + adres.toString() +
                '}';
    }
  1. 问题您必须创建一个LocalDate对象并将其传递给构造函数
public static void main(String[] args) {
        Address studentAddress = new Address("straat", 1, "postcode", "plaats")
        Student student = new Student(500739074, "Ronny", "Giezen", new LocalDate.now(), studentAddress); // for example
    }
© www.soinside.com 2019 - 2024. All rights reserved.