简体   繁体   English

如何给这个孩子 class 添加一个 arrayList?

[英]How to add an arrayList to this child class?

Context - So there are two classes that uses inheritance.上下文 - 所以有两个类使用 inheritance。 The EmployeeService is the parent class and the EmployeeInfo is the child class. EmployeeService 是父 class,EmployeeInfo 是子 class。

What do I need help with - So I am trying to insert an arrayList to the parent class that combines the information of the experience and position and makes a new arrayList called serviceList. What do I need help with - So I am trying to insert an arrayList to the parent class that combines the information of the experience and position and makes a new arrayList called serviceList.

And when I call a super() in the child class, I should be able to call the arrayList rather than the String variables (experience, position).当我在子 class 中调用 super() 时,我应该能够调用 arrayList 而不是字符串变量(经验、职位)。

To put it short, I should basically be able to pass an arrayList as the third parameter in the child class employeeInfo method instead of String experience or String position简而言之,我基本上应该能够通过 arrayList 作为子 class 员工信息方法中的第三个参数,而不是字符串经验或字符串 position

Parent class -父 class -

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

Child class -儿童 class -

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

Just add the property and an additional constructor to the parent class as follows:只需将属性和附加构造函数添加到父 class 如下:

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

Then adapt your child class:然后调整您的孩子 class:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM