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

Java 静态建议。我做错了什么?,java,static,Java,Static,所以我试图将ArrayList传递给我的主方法,但eclipse告诉我需要将ArrayList更改为静态。我知道我做错了什么,但我想不出来 ArrayList<Patient> pList = Doctor.getPatientList(); ArrayList pList=Doctor.getPatientList(); 这是我在main方法中的调用 public class Doctor { public ArrayList<Patient> patientLis

所以我试图将ArrayList传递给我的主方法,但eclipse告诉我需要将ArrayList更改为静态。我知道我做错了什么,但我想不出来

ArrayList<Patient> pList = Doctor.getPatientList();
ArrayList 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;
}
公立医生{
public ArrayList patientList=新建ArrayList();
}
公共void loadPatientData(){
BufferedReader read2=null;
试试{
read2=新的BufferedReader(新的文件阅读器(“data/patient_list.txt”);
弦线;
而((line=read2.readLine())!=null){
line=read2.readLine();
如果(行==null){
打破
}
String[]lineValues=line.split(,“”;//将此值上的字符串拆分为数组
String firstName=lineValues[0];
字符串lastName=lineValues[1];
字符串地址=行值[2];
字符串city=lineValues[3];
字符串状态=行值[4];
字符串zip=lineValues[5];
字符串ssn=行值[6];
字符串gendernedsconvert=lineValues[7];
字符串权重needsDouble=lineValues[8];
字符串高度needsDouble=lineValues[9];
String symptomsnotery=lineValues[10];//这些需要进一步分解(使用分号)
char gender=gendernedsconvert.charAt(0);
double weight=double.parseDouble(weightsneedsdouble);
双倍高度=双倍.parseDouble(高度需要双倍);
患者=新患者(名字、姓氏、地址、城市、州、邮编、ssn、性别、体重、身高、症状);
patientList.add(patient);//必须是患者类型。
}
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
if(read2!=null){
试一试{
read2.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
}
公共阵列列表getPatientList(){
返回病人名单;
}
这是我的博士课程的缩短版

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);


}
公共类患者{
私人字符串patientID;
私有字符串名;
私有字符串lastName;
私有字符串ssn;
私人性别;
私有字符串地址;
私人城市;
私有字符串状态;
私家弦征;
私人字符串拉链;
public ArrayList Diagnostics=新建ArrayList();
//私有诊断诊断=新诊断(0,null);//新诊断称为诊断数据类型
public ArrayList newmedicing=new ArrayList();
//私人药物NewMedicing=新药物(0,null);//新药物称为药物数据类型
ArrayList症状=新建ArrayList();
ArrayList SymphonCompare=新的ArrayList();
私人双倍重量;
私人双高;
int k=0;
公共患者(字符串名、字符串名、字符串地址、字符串城市、字符串州、字符串邮编、字符串ssn、字符性别、双倍体重、双倍身高、字符串症状){
this.firstName=firstName;
this.lastName=lastName;
这个.ssn=ssn;
这个。性别=性别;
重量=重量;
高度=高度;
this.address=地址;
this.city=城市;
this.state=状态;
这个。症状=症状;
this.zip=zip;
this.patientID=ssn.replace(“-”,“”);//从ssn中删除破折号并将值设置为patientID
this.patientID=this.patientID.replace(“,”);//从patientID中删除空格
this.patientID=this.patientID+this.firstName.substring(0,1)+this.lastName.substring(0,1);
}

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

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

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

否则,必须使函数为静态。

必须先声明类对象,然后调用其函数。例如:

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

否则必须将函数设为静态。

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

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

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

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

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

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

静态
修饰符与
公共数组列表getPatientList()一起使用

publicstaticArrayList getPatientList()


您正在类
医生
上调用此方法,因此此方法必须声明为静态。

使用
静态
修饰符和
公共数组列表getPatientList()

publicstaticArrayList getPatientList()


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

Doctor
类中,
patientList
loadPatientData()
getPatientList()
成员都是“实例”类
Doctor
的成员,这意味着您需要
Doctor
类型的实例或对象

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

Doctor doc = new Doctor();
doc.getPatientList();
静态
成员使用