有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么我会收到“学习记录”。Student@25a43blb'尝试显示链接列表中的所有对象时?

创建学生对象并将其添加到链接列表后,我尝试使用管理器类中的displayAll()方法显示它,但出于某种原因,这是我在输出时得到的:studentrecords.Student@25a43blb但我不知道它的含义

如果有人能在这里帮助我,我将不胜感激。求你了

所有代码如下:

StudentRecords(GUI)类:

import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.util.Queue;
import javax.swing.*;

public class StudentRecords implements ActionListener {

    private JTextField firstNameTextField;
    private JTextField surnameTextField;
    private JTextField yearOfStudyTextField;
    private JTextField emailTextField;
    private JTextField studentIdTextField;
    private JTextField moduleCodeTextField;
    private JTextField moduleMarkTextField;
    private JButton addStudentButton;
    private JButton addModuleButton;
    private JButton displayStudentButton;
    private JButton displayAllButton;
    private JButton displayMarksButton;
    private JButton deleteStudentButton;
    private JFrame frame;
    private Student student;
    private Manager manage = new Manager();

    public StudentRecords() {

        frame = new JFrame("Student Records");
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(9, 3));

        JLabel firstNameLabel = new JLabel("First Name:");
        contentPane.add(firstNameLabel);

        JLabel surnameLabel = new JLabel("Surname:");
        contentPane.add(surnameLabel);

        JLabel nothing = new JLabel(" ");
        contentPane.add(nothing);

        firstNameTextField = new JTextField(15);
        contentPane.add(firstNameTextField);

        surnameTextField = new JTextField(15);
        contentPane.add(surnameTextField);

        addStudentButton = new JButton("Add Student");
        contentPane.add(addStudentButton);
        addStudentButton.addActionListener(this);

        JLabel yearOfStudyLabel = new JLabel("Year of Study:");
        contentPane.add(yearOfStudyLabel);

        JLabel emailLabel = new JLabel("Email:");
        contentPane.add(emailLabel);

        JLabel nothing1 = new JLabel(" ");
        contentPane.add(nothing1);

        yearOfStudyTextField = new JTextField(15);
        contentPane.add(yearOfStudyTextField);

        emailTextField = new JTextField(15);
        contentPane.add(emailTextField);

        JLabel nothing2 = new JLabel(" ");
        contentPane.add(nothing2);

        JLabel studentIdLabel = new JLabel("Student ID:");
        contentPane.add(studentIdLabel);

        JLabel nothing3 = new JLabel(" ");
        contentPane.add(nothing3);

        JLabel nothing4 = new JLabel(" ");
        contentPane.add(nothing4);

        studentIdTextField = new JTextField(15);
        contentPane.add(studentIdTextField);

        displayStudentButton = new JButton("Display Student");
        contentPane.add(displayStudentButton);
        displayStudentButton.addActionListener(this);

        displayMarksButton = new JButton("Display Marks");
        contentPane.add(displayMarksButton);
        displayMarksButton.addActionListener(this);

        JLabel moduleCodeLabel = new JLabel("Module Code:");
        contentPane.add(moduleCodeLabel);

        JLabel moduleMarkLabel = new JLabel("Module Mark:");
        contentPane.add(moduleMarkLabel);

        JLabel nothing5 = new JLabel(" ");
        contentPane.add(nothing5);

        moduleCodeTextField = new JTextField(15);
        contentPane.add(moduleCodeTextField);

        moduleMarkTextField = new JTextField(15);
        contentPane.add(moduleMarkTextField);

        addModuleButton = new JButton("Add Module");
        contentPane.add(addModuleButton);
        addModuleButton.addActionListener(this);

        displayAllButton = new JButton("Display All");
        contentPane.add(displayAllButton);
        displayAllButton.addActionListener(this);

        JLabel nothing6 = new JLabel(" ");
        contentPane.add(nothing6);

        deleteStudentButton = new JButton("Delete Student");
        contentPane.add(deleteStudentButton);
        deleteStudentButton.addActionListener(this);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        StudentRecords sRecord = new StudentRecords();
    }

    public void actionPerformed(ActionEvent event) {
        String command = event.getActionCommand();
        if (command.equals("Add Student")) {
            addStudent();
        }
        if (command.equals("Add Module")) {
            addModule();
        }
        if (command.equals("Display All")) {
            displayAll();
        }
        if (command.equals("Display Student")) {
            displayStudent();
        }
        if (command.equals("Display Marks")) {
            displayMarks();
        }
        if (command.equals("Delete Student")) {
            deleteStudent();
        }
    }

    public String getFirstName() {
        String firstName
                = firstNameTextField.getText();
        return firstName;
    }

    public String getSurname() {
        String surname
                = surnameTextField.getText();
        return surname;
    }

    public int getYearOfStudy() {
        int yearOfStudy
                = Integer.parseInt(yearOfStudyTextField.getText());
        return yearOfStudy;
    }

    public String getEmail() {
        String email
                = emailTextField.getText();
        return email;
    }

    public int getStudentId() {
        int studentId
                = Integer.parseInt(studentIdTextField.getText());
        return studentId;
    }

    public String getModuleCode() {
        String moduleCode
                = moduleCodeTextField.getText();
        return moduleCode;
    }

    public int getModuleMark() {
        int moduleMark
                = Integer.parseInt(moduleMarkTextField.getText());
        return moduleMark;
    }

    public void addStudent() {

        Student studentObj = new Student(getFirstName(), getSurname(), getEmail(), getYearOfStudy(), getStudentId());
        manage.addStudent(studentObj);
    }

    public void addModule() {
        //search for the student withh the ID
        Student module = new Student(getFirstName(), getSurname(), getEmail(), getYearOfStudy(), getStudentId());
        module.addModule(getStudentId(), getModuleCode(), getModuleMark());
    }

    public void displayAll() {

        manage.displayAll();

    }

    public void displayStudent() {


    }

    public void displayMarks() {


    }

    public void deleteStudent() {


    }

}

学生班级:

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;

/**
 *
 * @author azim1
 */
public class Student {

    private String firstName;
    private String surname;
    private String email;
    private int yearOfStudy;
    private int studentId;
    private Queue<Module> mods = new LinkedList<>();

    public Student(String firstName, String surname, String email, int yearOfStudy, int studentId) {

        this.firstName = firstName;
        this.surname = surname;;
        this.email = email;
        this.yearOfStudy = yearOfStudy;;
        this.studentId = studentId;

    }

    public String getFirstName() {
        return firstName;
    }

    public String getSurname() {
        return surname;
    }

    public String getEmail() {
        return email;
    }

    public int getYearOfStudy() {
        return yearOfStudy;
    }

    public int getStudentId() {
        return studentId;
    }

    public String print() {
        return "Student ID: " + studentId + "\n"
                + "First Name: " + firstName + "\n"
                + "Surname: " + surname + "\n"
                + "Email: " + email + "\n"
                + "Year of Study: " + yearOfStudy;
    }

    public void addModule(int id, String mCode, int mMark) {
        if (mods.size() == 4) {
            mods.remove();
        }

        Module mod = new Module();
        mods.add(mod);
    }

    //this method returns the module list of this student sorted by marks
    public String getModulesSortedByMarks(Student id) {
        Object[] sortedMods;
        sortedMods = mods.toArray();

        Arrays.sort(sortedMods);

        String sortedModulesList = "";
        for (int i = 0; i < sortedMods.length; i++) {
            sortedModulesList = "\n" + ((Module) sortedMods[i]).print();
        }
        return sortedModulesList;

    }
}

模块类别:

public class Module implements Comparable {

    private String moduleCode;
    private int moduleMark;

    public String getModuleCode() {
        return moduleCode;
    }

    public int getModuleMark() {
        return moduleMark;
    }

    public String print() {
        return "Module Code: " + moduleCode + "\n"
                + "Mark: " + moduleMark;
    }

    @Override
    public int compareTo(Object aModule) {
        if (this.moduleMark == ((Module) aModule).getModuleMark()) {
            return 0;
        } else if (this.moduleMark < ((Module) aModule).getModuleMark()) {
            return -1;
        } else {
            return 1;
        }
    }

}

经理课程:

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author azim1
 */
public class Manager {

    private LinkedList<Student> listOfStudents = new LinkedList<>();


    public void addStudent(Student studentObj) {

        listOfStudents.add(studentObj);
    }

    public void displayStudent(Student studentId) {

        Iterator<Student> it = listOfStudents.iterator();
        while (it.hasNext()) {
            Student s = it.next();
            if (s.equals(studentId)) {
                System.out.println(s);
            }
        }
    }

    public void displayMarks(Student studentId) {


    }

    public void deleteStudent(Student studentId) {

        Iterator<Student> it = listOfStudents.iterator();
        while (it.hasNext()) {
            if (it.next().equals(studentId)) {
                it.remove();
            }
        }
    }

    public void displayAll() {

        Iterator<Student> it = listOfStudents.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }
}

共 (2) 个答案

  1. # 1 楼答案

    问题在于displayAll()函数只是返回类的“引用”(基本上是表示对象位置的唯一ID)。如果不想返回学生的内容,则必须定义一个toString()方法,该方法将告诉类在需要时返回对象的字符串表示形式(如System.out.print(myObject)

    学生。java

    public String toString() {
        return print(this.firstName + " " + this.surname); // include whatever else you want in the string, here I only include the name and surname
    }
    

    有关更多详细信息,请参见link

  2. # 2 楼答案

    您的Student类需要定义一个toString()方法,该方法返回对象的字符串表示形式

    public String toString() {
        return print(); // if you want to reuse the output from your print method
    }
    

    Object中的默认toString方法输出类的短名称和对象的引用