java - 静态建议。我究竟做错了什么?

标签 java static

所以我试图将 ArrayList 传递给我的 main 方法,但 eclipse 告诉我我需要将 arraylist 更改为静态。我知道我做错了什么,但我无法弄清楚。

ArrayList<Patient> pList = Doctor.getPatientList();

这是我在主方法中进行的调用。

public class Doctor {
public ArrayList<Patient> patientList = new ArrayList<Patient>();
}

public void loadPatientData() {
    BufferedReader read2 = null;

    try {   
        read2 = new BufferedReader(new FileReader("data/patient_list.txt"));
        String line;
        while ((line = read2.readLine()) != null) {
            line = read2.readLine();
            if (line == null) {
                break;
            }
            String[] lineValues = line.split(","); //split the string on this value into array
            String firstName = lineValues[0];
            String lastName = lineValues[1];
            String address = lineValues[2];
            String city = lineValues[3];
            String state = lineValues[4];
            String zip = lineValues[5];
            String ssn = lineValues[6];
            String genderNeedsConvert = lineValues[7];
            String weightNeedsDouble = lineValues[8];
            String heightNeedsDouble = lineValues[9];
            String symptomsNotReady = lineValues[10]; // these need to be broken up further (using semicolons)

            char gender = genderNeedsConvert.charAt(0);
            double weight = Double.parseDouble(weightNeedsDouble);
            double height = Double.parseDouble(heightNeedsDouble);

            Patient patient = new Patient(firstName, lastName, address, city, state, zip, ssn, gender, weight, height, symptomsNotReady); 
            patientList.add(patient); // must be of patient type.
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (read2 != null) {
            try {
                read2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
      }
    }
public ArrayList<Patient> getPatientList() {
    return patientList;
}

这是我的 Doctor 类(class)的简化版本。

public class Patient {

private String patientID;
private String firstName;
private String lastName;
private String ssn;
private char gender;
private String address;
private String city;
private String state;
private String symptoms;
private String zip;
public ArrayList<Diagnosis> diagnoses = new ArrayList<Diagnosis>();
//private Diagnosis diagnoses = new Diagnosis(0, null);// new diagnoses called as Diagnoses datatype
public ArrayList<Medication> newMedication = new ArrayList<Medication>();
//private Medication newMedication = new Medication(0, null);// newMedication called as medication datatype
ArrayList<String> symptom = new ArrayList<String>();
ArrayList<String> symptomCompare = new ArrayList<String>();
private double weight;
private double height;
int k = 0;


public Patient(String firstName,String lastName,String address,String city, String state,String zip,String ssn,char gender,double weight,double height,String symptoms){
    this.firstName = firstName;
    this.lastName = lastName;
    this.ssn = ssn;
    this.gender = gender;
    this.weight = weight;
    this.height = height;
    this.address = address;
    this.city = city;
    this.state = state;
    this.symptoms = symptoms;
    this.zip = zip;
    this.patientID = ssn.replace("-", ""); // removes dashes from ssn and sets the value to patientID
    this.patientID = this.patientID.replace(" ", ""); //removes spaces from patientID
    this.patientID = this.patientID + this.firstName.substring(0, 1) + this.lastName.substring(0, 1);


}

及以上是缩短的患者类别。我已经坐在这里几个小时尝试不同的事情,但它一直告诉我将 getPatientList() 方法更改为静态。我做错了什么?

最佳答案

Doctor.getPatientList() 是调用静态方法的语法,因为 Doctor 是一个类名。

如果您想调用实例方法(并且 getPatientList() 当前是一个实例方法),您应该使用 Doctor 的实例来调用它:

Doctor doctor = new Doctor();
ArrayList<Patient> pList = doctor.getPatientList();

关于java - 静态建议。我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27398329/

相关文章:

c++ - 如何通过静态结构数组专门化模板函数

c - sleep() 对 c 中静态变量的影响

java - 如何更新这个只允许用户猜测 5 次的方法?

java - JPA-存储库保存 : Proceed saving after insert Constraint violation

java - 线程 "Basic L&F File Loading Thread"中出现异常

java - 如何在java中将文本放入pdfbox的矩形中?

java - 如何在 Collections.forEach() 中打印计数

Java、泛型和静态方法

Java - 不实例化对象时是否需要 static 和 volatile?

c - 关于C中static关键字的两个问题