2

我对子类有点困惑。这是我的代码:

public class MedHistory {

    private String grafts;
    private String allergies;
    private String diseases;
    private String surgeries;
    private String medicalTreatment;

    //Constructors (#2)

    public MedHistory(String allergies, String diseases, String grafts,
            String treatments, String surgeries) {
        this.allergies=allergies;
        this.diseases=diseases;
        this.grafts=grafts;
        this.medicalTreatment=treatments;
        this.surgeries=surgeries;
    }

    public MedHistory() {
        this.allergies="";
        this.diseases="";
        this.grafts="";
        this.medicalTreatment="";
        this.surgeries="";
    }

    //Getters 
    public String getGrafts() {
        return grafts;
    }

    public String getAllergies() {
        return allergies;
    }

    public String getDiseases() {
        return diseases;
    }

    public String getSurgeries() {
        return surgeries;
    }

    public String getMedicalTreatment() {
        return medicalTreatment;
    }

    //Setters 

    public void setGrafts(String grafts) {
        this.grafts = grafts;
    }

    public void setAllergies(String allergies) {
        this.allergies = allergies;
    }

    public void setDiseases(String diseases) {
        this.diseases = diseases;
    }

    public void setSurgeries(String surgeries) {
        this.surgeries = surgeries;
    }

    public void setMedicalTreatment(String medicalTreatment) {
        this.medicalTreatment = medicalTreatment;
    }

    public class FemMedHistory extends MedHistory {

        private List<Birth> births = new ArrayList<Birth>();

        //Constructors (#2)

        public FemMedHistory(String allergies, String diseases, String grafts,String treatments, String surgeries, List<Birth> birthlist) {
            super(allergies,allergies,grafts,treatments,surgeries);
            this.births=birthlist;
        }

        public FemMedHistory() {
            super();
            this.births=null;
        }

        //Getter

        public List<Birth> getBirths() {
            return this.births;

        }

        //Setter

        public void setBirths(List<Birth> list) {
            this.births=list;
        }

    }
}

当我尝试像这样创建一个新的 FemMedHistory 对象时:

List<Birth> list = new ArrayList<Birth>();
list.add(new Birth(new GregorianCalendar(2011,4,10),"kaisariki",4));
FemMedHistory female = new FemMedHistory("allergia2","astheneia2","emvolia2","farmekeutiki agwgi2", "xeirourgeia2", list);

我得到错误:

无法访问 MedHistory 类型的封闭实例。必须使用 MedHistory 类型的封闭实例来限定分配(例如,xnew A(),其中 x 是 MedHistory 的实例)。

那么,使用子类的正确方法是什么?

4

3 回答 3

5

当您声明一个nested类时,它只能通过 Outer 类使用。

要在外部访问它,您需要将 FemMedHistory 类设为静态。

public static class FemMedHistory extends MedHistory {...}

通过 MedHistory 类访问它

MedHistory.FemMedHistory myMedHistory = ...

或在它自己的 Java 文件中声明它。

于 2013-05-29T19:52:11.687 回答
3

您已将子类声明为内部类,这意味着如果不先创建包含类的实例,就无法创建它的实例。

解决此问题的最常见方法是将其声明为单独的类,这样可以消除您的错误。

于 2013-05-29T19:51:42.977 回答
3

长话短说:剪切所有 FemMedHistory代码并将其粘贴到FemMedHistory.java. 现在,您已经涉及到您尚未掌握的 Java 概念。此外,该类确实属于单独的文件。

于 2013-05-29T19:54:35.803 回答