gpt4 book ai didi

java - 当我尝试添加一个 ID 时,我所有的 ID 都会增加

转载 作者:行者123 更新时间:2023-11-29 07:09:00 24 4
gpt4 key购买 nike

我正在尝试在创建患者时添加患者 ID,该 ID 每次都会递增 1。但是当运行我的代码时,它也会增加所有其他 ID 的 ID。

    JButton btnCp = new JButton("Create Patient");
btnCp.addActionListener(new ActionListener() {//add action listener to btnCp (Create Patient Button)
public void actionPerformed(ActionEvent arg0) {

name = pateintNameTextField.getText();//reads entered text
address = patientAddressTextField.getText();
phone = patientPhoneTextField.getText();

pateintNameTextField.setText("");//sets fields back to empty ("")
patientAddressTextField.setText("");
patientPhoneTextField.setText("");




Patient.setID(Patient.patientNum +1);


Patient patient = new Patient(name,address,phone);//adds the Patient to the patient class
patientListModel.addElement(patient);//adds patient to patientLst
Patient.aPatientList.add(patient);

我的 Patient 类如下:

public class Patient implements Serializable{

public static int patientNum;
public String patientName;
public String patientAddress;
public String patientPhone;


public static ArrayList<Patient> aPatientList = new ArrayList<Patient>();



public Patient(String n, String a, String p){


patientName = n;
patientAddress = a;
patientPhone = p;


}

public static void setID(int id){

patientNum = id;



}

public static int getID(){

return patientNum;

}


public void setName(String n){

patientName = n;



}

public String getName(){

return patientName;

}

public void setAddress(String a){

patientAddress = a;


}

public String getAddress(){

return patientAddress;
}



public void SetPhoneNum(String p){
patientPhone = p;

}

public String getPhoneNum(){

return patientPhone;
}

public String toString() {
return "ID: " +getID() + " Name: "+ getName() +" Address: "+getAddress() +" Phone Number: "+getPhoneNum();
}




}

然后我还将患者写入 jList。提前致谢

最佳答案

由于您已将 patientNum 声明为 static,因此即使您增加其中一个,所有 patientNum 都会增加,因为它由对象。

关于java - 当我尝试添加一个 ID 时,我所有的 ID 都会增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16253383/

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