嗨,我在我的一个类中创建了一个toString方法,如下所示。
学生班:

package Practical5;

public class Student extends Person {

    //instance variables
    private static int MAX_MODULES = 6;
    private StudentMode modeOfStudy;
    private boolean studentLoan;
    private int numEnrolledModules;

    //constructor
    public Student(String name, String dob, Address address, StudentMode modeOfStudy, boolean studentLoan) {
        super(name, dob, address);
    this.modeOfStudy = modeOfStudy;
    this.studentLoan = studentLoan;
    this.numEnrolledModules = 0;
    }

    //accessors & mutators
    public StudentMode getMode() {
        return modeOfStudy;
    }

    public boolean isStudentLoan() {
        return studentLoan;
    }

    public int getNumEnrolledModules() {
        return numEnrolledModules;
    }

    public void setMode(StudentMode modeOfStudy) {
        this.modeOfStudy = modeOfStudy;
    }

    public void setStudentLoan(boolean studentLoan) {
        this.studentLoan = studentLoan;
    }

    public void setNumEnrolledModules(int numEnrolledModules) {
        this.numEnrolledModules = numEnrolledModules;
    }

    @Override
    public void purchaseParkingPass() {
        System.out.println(getName() + " just purchased a parking pass with student discount.");
    }

    @Override
    public void addModule(String moduleCode) {
        if (getNumEnrolledModules() < MAX_MODULES) {
            System.out.println(getName() + " successfully registered for the module: " + moduleCode);
        }
        else {
            System.out.println("You are unable to register for " + moduleCode + " as the maximum number of permitted module enrolments has been reached.");
        }

    }

    public String toString() {
        return "Student [ ID: " + getId() + "; Name: " + getName() +
                "; DOB: " + getDob() + "; Study Mode: " + getMode() +
                "; Number of Enrolled Modules: " + getNumEnrolledModules();
    }
}


人员类别:

package Practical5;

public abstract class Person {


    //instance variables
    private static int LAST_ID = 1000 + 1;
    private int id;
    private String name;
    private String dob;
    private Address address;

    //constructor
    public Person(String name, String dob, Address address) {
        super();
        LAST_ID ++;
        this.id = LAST_ID;

    }

    //accessors & mutators
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDob() {
        return dob;
    }

    public Address getAddress() {
        return address;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    //methods
    public abstract void purchaseParkingPass();
    public abstract void addModule(String moduleCode);



}


然后,我创建了一个测试器类,并创建了一个新的ArrayList并将这些元素添加到其中。

然后,我创建了一个for循环,以循环遍历每个元素并调用toString方法以打印出每个元素的详细信息,但它返回的是空值。

测试人员类别:
    实用包5;

import java.util.ArrayList;
import java.util.Scanner;



public class UIS_Tester {

    public static void main(String[] args) {

        Student student1 = new Student("James Black", "07/09/1995" , new Address("Wheeler's Road",10,"Belfast", "BT12 5EG", "Co.Antrim"),StudentMode.Fulltime, false);
        Student student2 = new Student("Adam Smith", "12/11/1979" , new Address("Ivy Hill",67,"Belfast", "BT17 7BN", "Co.Antrim"),StudentMode.Parttime, true);

        ArrayList<Person> uniPeople = new ArrayList<Person>();

        uniPeople.add(student1);
        uniPeople.add(student2);

        printMenu(uniPeople);



    }


    public static void printAllDetails(ArrayList<Person> uniPeople) {
        for (int i = 0; i < uniPeople.size(); i++) {
            System.out.println(uniPeople.get(i).toString());
        }
    }
}


输出:

Student [ ID: 1002; Name: null; DOB: null; Study Mode: Fulltime; Number of Enrolled Modules: 0
Student [ ID: 1003; Name: null; DOB: null; Study Mode: Parttime; Number of Enrolled Modules: 0


谁能帮我解决这个问题?谢谢

最佳答案

您必须将名称值存储在构造函数中。您的版本未使用名称值。

public Person(String name, String dob, Address address) {
    super();
    this.name = name; // <== important line
    this.dob = dob;  // <== important line
    this.address = address;  // <== important line
    LAST_ID ++;
    this.id = LAST_ID;

}

关于java - 打印出arrayList时是否为空值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36518166/

10-13 05:25