Java中自定义Doctor类初始化ArrayList报错问题求助
问题分析与解决方案
你的代码出现两个核心问题:
- 类定义顺序错误:在
main方法中先使用了Doctor类,但该类是在使用代码之后才定义的,Java编译器需要先看到类定义才能识别它。 - 局部内部类的修饰符限制:
Doctor是定义在main方法内的局部内部类,这类类不能使用public、private等访问修饰符,加public必然触发"非法修饰符"错误。 - 嵌套结构不合理:
Patient类嵌套在Doctor类内部,导致创建患者实例必须依赖医生实例,不符合业务逻辑。
以下是两种可行的修复方案:
方案一:改为顶级类(推荐,适合作业代码结构)
把Doctor和Patient从AandEDepartment类中独立出来,作为顶级类,结构更清晰,符合Java常规代码组织方式:
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.*; // 独立的Doctor类 class Doctor { String name; String specialisation; public Doctor(String name, String specialisation){ this.name = name; this.specialisation = specialisation; } public String getName() { return name; } public String getSpecialisation() { return specialisation; } } // 独立的Patient类 class Patient { int patientID; String patientFirstName; String patientLastName; int patientAge; String condition; double cost; public Patient (int patientID, String patientFirstName, String patientLastName, int patientAge, String condition, double cost) { this.patientID = patientID; this.patientFirstName = patientFirstName; this.patientLastName = patientLastName; this.patientAge = patientAge; this.condition = condition; this.cost = cost; } // 按需补充Patient的getter方法 } public class AandEDepartment { public static void main(String[] args) { ArrayList<Doctor> doctors = new ArrayList<>(); doctors.add(new Doctor("James", "Cardiovascular")); doctors.add(new Doctor("Richard", "Dermatology")); doctors.add(new Doctor("Peter", "Orthopedics")); // 正常使用HashMap关联医生与患者 HashMap<Doctor, List<Patient>> doctorPatientMap = new HashMap<>(); // 示例:给James添加患者 Patient patient1 = new Patient(1, "Alice", "Smith", 30, "Chest Pain", 150.0); doctorPatientMap.put(doctors.get(0), new ArrayList<>(List.of(patient1))); } }
方案二:保留内部类(调整位置和修饰符)
如果必须把Doctor和Patient放在AandEDepartment类内部,需将它们移到main方法外,设为静态成员内部类(避免依赖外部类实例):
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.*; public class AandEDepartment { // 静态成员内部类Doctor,无需外部类实例即可创建 static class Doctor { String name; String specialisation; public Doctor(String name, String specialisation){ this.name = name; this.specialisation = specialisation; } public String getName() { return name; } public String getSpecialisation() { return specialisation; } } // 静态成员内部类Patient static class Patient { int patientID; String patientFirstName; String patientLastName; int patientAge; String condition; double cost; public Patient (int patientID, String patientFirstName, String patientLastName, int patientAge, String condition, double cost) { this.patientID = patientID; this.patientFirstName = patientFirstName; this.patientLastName = patientLastName; this.patientAge = patientAge; this.condition = condition; this.cost = cost; } // 按需补充Patient的getter方法 } public static void main(String[] args) { ArrayList<Doctor> doctors = new ArrayList<>(); doctors.add(new Doctor("James", "Cardiovascular")); doctors.add(new Doctor("Richard", "Dermatology")); doctors.add(new Doctor("Peter", "Orthopedics")); // 正常使用HashMap关联 HashMap<Doctor, List<Patient>> doctorPatientMap = new HashMap<>(); } }
内容的提问来源于stack exchange,提问作者Nate
相关产品推荐
相关产品推荐

