gpt4 book ai didi

java - 给java类赋值,但不返回值

转载 作者:行者123 更新时间:2023-12-01 18:53:17 25 4
gpt4 key购买 nike

所以我有一个学生类。

我有一个 Jlist 并尝试添加一个学生对象。我设置了dob值。

一切正常,没有问题。当我检索 sout“student”时,它返回所有其他值,但不返回 Dob。当我调用 Student.getDob() 实际上它会返回,意味着它已被分配。

不知道为什么它不将学生作为对象检索。我想也许 netbeans 中有一个错误。

有什么帮助吗?

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          

// Create an instance of student
Student student = new Student();
student.setFName(firstNameField.getText());
student.setLName(lastNameField.getText());
student.setDob(dateOfBirthField.getText());
student.setGender((String) genderComboBox.getSelectedItem());
System.out.println(student.getDob());

// Add student to the list
ListModel lModel = jList1.getModel();
DefaultListModel dlm = new DefaultListModel<Object>();
int total = lModel.getSize();
for (int x = 0; x < total; x++) {
dlm.addElement(lModel.getElementAt(x));
}
dlm.addElement(student);
jList1.setModel(dlm);
//Clearing the text fields for the next entry
firstNameField.setText("");
lastNameField.setText("");
dateOfBirthField.setText("");
genderComboBox.setSelectedIndex(0);}
}

学生类(class):

import java.util.ArrayList; 
import java.util.Date;

public class Student {

private String id;


private String fName;


private String lName;

private String gender;

private String dob;

private ArrayList<Address> addresses;

private Instructor instructor;


private ArrayList<Courses> courses;


public Student () {
}


public ArrayList<Address> getAddresses () {
return addresses;
}


public void setAddresses (ArrayList<Address> val) {
this.addresses = val;
}


public ArrayList<Courses> getCourses () {
return courses;
}


public void setCourses (ArrayList<Courses> val) {
this.courses = val;
}


public String getDob () {
return dob;
}


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


public String getFName () {
return fName;
}

public void setFName (String val) {
this.fName = val;
}


public String getGender () {
return gender;
}


public void setGender (String val) {
this.gender = val;
}


public String getId () {
return id;
}


public void setId (String val) {
this.id = val;
}


public Instructor getInstructor () {
return instructor;
}

public void setInstructor (Instructor val) {
this.instructor = val;
}

public String getLName () {
return lName;
}


public void setLName (String val) {
this.lName = val;
}
@Override
public String toString(){
return getFName()+" "+getLName()+" "+getGender();
}
}

最佳答案

我通过深度调试发现了它。在学生类,多布没有被拉弦。最后添加了 getDob() 。

@Override
public String toString(){
return getFName()+" "+getLName()+" "+getGender()+" "+getDob();
}

关于java - 给java类赋值,但不返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59699020/

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