[英]How to pass a List from an abstract Class to another class?
Employee is my abstract class and it has FixedPosition as a daughter class. Employee是我的抽象 class,它有FixedPosition作为子 class。 Company has a method called addEmployee() that must receive a type Employee and then add the List from Employee to its own List.
Company 有一个名为 addEmployee() 的方法,该方法必须接收一个 Employee 类型,然后将来自 Employee 的 List 添加到它自己的 List 中。
Company company = new Company("Hi");
List<Employee> employees = new ArrayList<>();
Later I fill up my List but I don't know how to send a type Employee into the method from Company.后来我填写了我的列表,但我不知道如何将类型 Employee 从公司发送到方法中。
employees.add(new FixedPosition(name, occupation, salary, monthsContract));
company.addEmployee();
Your method addEmployee()
has no argument.您的方法
addEmployee()
没有参数。 Rewrite it so that it gets an employee object solves your problem.重写它,让它得到一个员工 object 解决了你的问题。
public void addEmployee( Employee e ) {
this.employees.add(e);
}
Your company class should have the list of employees.您的公司 class 应该有员工名单。
It seems that you need to overload method addEmployee
in your Company
class to support handling of an employee list:您似乎需要在
Company
class 中重载方法addEmployee
以支持处理员工列表:
public void addEmployee(List<Employee> employees) {
this.employees.addAll(employees);
}
Or you can call existing Company
method with a single employee in a loop:或者,您可以在循环中使用单个员工调用现有的
Company
方法:
employees.forEach(company::addEmployee);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.