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

时间:2014-12-10 10:14:17

标签: java static

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

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

这是我在main方法中的调用。

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类的缩短版本。

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()方法更改为静态。我做错了什么?

5 个答案:

答案 0 :(得分:3)

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

如果要调用实例方法(并且getPatientList()当前是实例方法),则应使用Doctor的实例调用它:

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

答案 1 :(得分:1)

您声称为static的所有变量/方法都称为类成员。严格地说,它们属于类而不是对象的属性。

当变量为static时,它甚至在创建对象之前就存在。那意味着什么呢?

  • 静态方法可以访问静态变量/方法。
  • 静态方法无法访问非静态变量/方法。 (因为它们不存在)

如果要让静态方法访问非静态变量/方法。其中一种方法是实例化(创建)您想要访问的方法/变量首先属于该对象的对象。

在您可以访问它之前需要首先实例化的原因是因为您希望首先使它们存在。类本身只是一个蓝图,您需要创建一个对象(使它们存在),然后才能与它进行交互。

示例:

Doctor doctor = new Doctor();
ArrayList<Patient> list = doctor.patientList;  
//Only possible if patientList is not private

如果在Class Doctor中,patientList是私有的,则需要使用getter:

Doctor doctor = new Doctor();
ArrayList<Patient> list = doctor.getPatientList();  
//getPateientList is a method in Doctor class

答案 2 :(得分:0)

您必须先声明类对象,然后调用其函数。喜欢:

Doctor doc = new Doctor();
doc.getPatientList();

否则你必须使该功能静止。

答案 3 :(得分:0)

static修饰符与public ArrayList<Patient> getPatientList()

一起使用

public static ArrayList getPatientList()

您正在类Doctor上调用此方法,因此必须将此方法声明为static。

答案 4 :(得分:0)

Doctor班级中,patientListloadPatientData()getPatientList()成员都是班级Doctor的“实例”成员,这意味着您需要类型为Doctor的实例或对象。

因此,要调用getPatientList(),您需要创建一个Doctor对象,如下所示:

Doctor doc = new Doctor();
doc.getPatientList();
使用类的名称访问

static成员,其中使用对象的名称访问实例成员。