【问题标题】:How to add an arrayList to this child class?如何将 arrayList 添加到这个子类?
【发布时间】:2022-01-05 21:50:42
【问题描述】:

上下文 - 所以有两个使用继承的类。 EmployeeService 是父类,EmployeeInfo 是子类。

我需要什么帮助-所以我试图将一个arrayList插入到父类中,它结合了经验位置的信息并制作了一个新的arrayList称为服务列表。

当我在子类中调用 super() 时,我应该能够调用 arrayList 而不是 String 变量(经验、位置)。

简而言之,我应该基本上可以在子类employeeInfo方法中传递一个arrayList作为第三个参数,而不是String经验或String位置

父类-

public class EmployeeService () {
    private String experience;
    private String position;
    
    public EmployeeService (String experience, String position) {
        this.setExperience (experience);
        this.setPosition(position);
    }
    
    public String getExperience() {
        return experience;
    }

    public void setExperience(String experience) {
        this.experience = experience;
    }
    
    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }
    
    public String toString() {
        return "Experience - " + experience + "Position" + " - " + position;
    }
    
}

儿童班-

public class EmployeeInfo () {
    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);
    }
    
    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();
    }
}

【问题讨论】:

  • 请说明您的意图。你的主类甚至没有按原样编译,你的描述很难理解。是否要将ListMap 添加到父类?
  • 我想要一个父类的列表。
  • 我确实在主类中添加了错误,因此使用 cmets 会更容易理解。
  • 但是你的主类中的employeeList 是什么?它似乎是一个List<EmployeeService>,所以你想要List<EmployeeService> 中的List<EmployeeService> 属性吗?
  • 它是一个hashMap,用于创建一个带有子类的新对象,而不是父类。这是作业的一部分,如果说明含糊,我很抱歉。我需要帮助的只是能够将 arrayList 从父类传递给子类。

标签: java class object inheritance arraylist


【解决方案1】:

只需将属性和附加构造函数添加到父类中,如下所示:

import java.util.ArrayList;
import java.util.List;

public class EmployeeService {
    private String experience;
    private String position;
    private List<String> serviceList;

    public EmployeeService (String experience, String position) {
        this.setExperience (experience);
        this.setPosition(position);
    }

    public EmployeeService (String experience, String position, List<String> serviceList) {
        this(experience, position);
        this.serviceList = new ArrayList<>(serviceList);
    }

    public String getExperience() {
        return experience;
    }

    public void setExperience(String experience) {
        this.experience = experience;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String toString() {
        return "Experience - " + experience + "Position" + " - " + position;
    }
}

然后调整您的子类:

import java.util.List;

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

    public EmployeeInfo (String firstName, String address, String experience, String position, List<String> serviceList) {
        super(experience, position, 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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    相关资源
    最近更新 更多