Java继承场景下如何实现子类传递ArrayList参数至父类
实现方案
父类修改内容
- 新增
ArrayList<String>类型成员变量serviceList,统一存储experience、position信息,保证两处数据同步 - 新增接收
ArrayList<String>参数的构造方法,完成参数校验后提取列表值赋值给对应属性 - 保留原有双String参数的构造方法,兼容历史调用逻辑,同时同步更新
serviceList
修改后完整父类代码:
import java.util.ArrayList; public class EmployeeService { private String experience; private String position; // 新增的ArrayList类型成员变量 private ArrayList<String> serviceList; // 原有构造方法保留,兼容历史调用 public EmployeeService (String experience, String position) { this.setExperience(experience); this.setPosition(position); // 同步更新serviceList this.serviceList = new ArrayList<>(); this.serviceList.add(experience); this.serviceList.add(position); } // 新增的接收ArrayList的构造方法 public EmployeeService (ArrayList<String> serviceList) { // 基础参数校验,避免空指针或下标越界 if (serviceList == null || serviceList.size() < 2) { throw new IllegalArgumentException("serviceList不能为空且至少包含2个元素"); } this.setExperience(serviceList.get(0)); this.setPosition(serviceList.get(1)); this.serviceList = serviceList; } public String getExperience() { return experience; } public void setExperience(String experience) { this.experience = experience; // 保证serviceList同步更新 if (this.serviceList != null && this.serviceList.size() >=1) { this.serviceList.set(0, experience); } } public String getPosition() { return position; } public void setPosition(String position) { this.position = position; // 保证serviceList同步更新 if (this.serviceList != null && this.serviceList.size() >=2) { this.serviceList.set(1, position); } } // 新增serviceList的getter public ArrayList<String> getServiceList() { // 返回副本避免外部修改内部属性 return new ArrayList<>(serviceList); } public String toString() { return "Experience - " + experience + " Position - " + position; } }
子类修改内容
- 新增重载构造方法,第三个参数为
ArrayList<String>类型,调用父类新增的列表构造方法完成初始化 - 保留原有四参数构造方法,不影响历史业务逻辑
修改后完整子类代码:
import java.util.ArrayList; public class EmployeeInfo extends EmployeeService { private String firstName; private String address; // 原有构造方法保留,兼容历史调用 public EmployeeInfo (String firstName, String address,String experience, String position) { super(experience, position); this.setFirstName(firstName); this.setAddress(address); } // 新增的接收ArrayList作为第三个参数的构造方法 public EmployeeInfo (String firstName, String address, ArrayList<String> serviceList) { super(serviceList); this.setFirstName(firstName); this.setAddress(address); } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String toString() { return "Name - " + firstName + " Address - " + address + " " + super.toString(); } }
调用示例
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> serviceList = new ArrayList<>(); serviceList.add("3年前端开发经验"); serviceList.add("前端开发工程师"); // 使用新增的构造方法传列表参数 EmployeeInfo employee = new EmployeeInfo("李四", "上海市浦东新区", serviceList); System.out.println(employee); // 输出结果:Name - 李四 Address - 上海市浦东新区 Experience - 3年前端开发经验 Position - 前端开发工程师 } }
内容的提问来源于stack exchange,提问作者beginner_101
相关产品推荐
相关产品推荐

