访问嵌套类的变量时出错

时间:2014-12-05 16:32:58

标签: java nested-class

我迫切需要帮助。我有两节课。 一个是带有嵌套类的PatientRecord。 另一个是PatientGenerator,它通过生成随机数据来填充带有PatientRecords的数据库 对于PatientRecord的每个字段。

我的问题是我无法访问某些嵌套类的变量,但我可以访问其他类。 (至少它会为某些人编译并因其他人的错误而失败。)

error: cannot find symbol
            temp.fname = getRFirstName();
  symbol:   variable fname
  location: variable temp of type PatientRecord.Visitor

这是失败的电话

private ArrayList<PatientRecord.Visitor> generateVisitors(PatientRecord p) {
    int payments = rnd.nextInt(10);
    ArrayList<PatientRecord.Visitor> array = new ArrayList<PatientRecord.Visitor>();
    for (int i = 0; i < payments; i++) {
        PatientRecord.Visitor temp = p.new Visitor();
        temp.fname = getRFirstName();
        temp.lname = getRLastName();
        temp.relation = relations[rnd.nextInt(relations.length)];
        array.add(temp);
    }
    return array;
}

这里是该类的一部分,其中包含嵌套的类Visitor。

public class PatientRecord implements Serializable{
   public int pId;
   public String FirstName;
   public String MiddleName;
   public String LastName;
   public boolean gender;
   public Location location;
   public ArrayList visitors;
   public ArrayList emergencyContacts;
   public DateTime discharge;
   public Admission admission;
   public String primaryDoctor;
   public ArrayList procedures;
   public ArrayList prescriptions;
   public ArrayList nurseNotes;
   public ArrayList doctorNotes;
   public InsurancePolicy insurancePolicy;
   public ArrayList billing; // Arraylist of payments


   public PatientRecord() {
       init();
   }

   public PatientRecord(int id) {
       pId = id;
       init();
   }

   private void init() {
       visitors = new ArrayList<Visitor>();
       emergencyContacts = new ArrayList<Contact>();
       location = new Location();
       discharge = new DateTime();
       admission = new Admission();
       procedures = new ArrayList<Procedure>();
       prescriptions = new ArrayList<Prescription>();
       nurseNotes = new ArrayList<Note>();
       doctorNotes = new ArrayList<Note>();
       billing = new ArrayList<Payment>();
       insurancePolicy = new InsurancePolicy();


   }

   public class Visitor {
        public String fname;
        public String lname;
        public String relation;
        public Visitor() {
        }
   }

1 个答案:

答案 0 :(得分:0)

我没有编译错误(在删除了一些代码之后):

public class PatientRecord
{

public int pId;
public String FirstName;
public String MiddleName;
public String LastName;
public boolean gender;
public ArrayList visitors;
public ArrayList emergencyContacts;
public String primaryDoctor;
public ArrayList procedures;
public ArrayList prescriptions;
public ArrayList nurseNotes;
public ArrayList doctorNotes;
public ArrayList billing; // Arraylist of payments

public PatientRecord() {
    init();
}

public PatientRecord(int id) {
    pId = id;
    init();
}

private void init() {
    visitors = new ArrayList<Visitor>();
}

public class Visitor
{
    public String fname;
    public String lname;
    public String relation;
}

public class PatientGenerator
{
    public ArrayList<PatientRecord.Visitor> generateVisitors(PatientRecord p) {
        //int payments = rnd.nextInt(10);
        ArrayList<PatientRecord.Visitor> array = new ArrayList<PatientRecord.Visitor>();
        for (int i = 0; i < 10; i++) {
            PatientRecord.Visitor temp = p.new Visitor();
            temp.fname = "first";
            temp.lname = "last";
            temp.relation = "brother";
            array.add(temp);
        }
        return array;
    }
}


public static void main(String[] args)
{
    PatientRecord t = new PatientRecord();
    PatientGenerator c = t.new PatientGenerator();
    c.generateVisitors(t);

}
}

您的代码是否与此类似?