Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何添加对象数组_Java_Oop - Fatal编程技术网

Java 如何添加对象数组

Java 如何添加对象数组,java,oop,Java,Oop,如何在Manager类中将Employee对象数组添加到Manager类,以及如何创建从Manager中添加和删除员工的方法 EmployeeTest.java package com.example; import com.example.domain.Employee; import com.example.domain.Engineer; import com.example.domain.Manager; import com.example.domain.Admin; import c

如何在Manager类中将Employee对象数组添加到Manager类,以及如何创建从Manager中添加和删除员工的方法

EmployeeTest.java

package com.example;
import com.example.domain.Employee;
import com.example.domain.Engineer;
import com.example.domain.Manager;
import com.example.domain.Admin;
import com.example.domain.Director;
import java.text.NumberFormat;

public class EmployeeTest {

public static void main(String[] args) {

    // Create the classes as per the practice
    Engineer eng = new Engineer(101, "Jane Smith", "012-34-5678", 120_345.27);

    Manager mgr = new Manager(207, "Barbara Johnson", "054-12-2367", 109_501.36, "US Marketing");

    Admin adm = new Admin(304, "Bill Munroe", "108-23-6509", 75_002.34);

    Director dir = new Director(12, "Susan Wheeler", "099-45-2340", 120_567.36, "Global Marketing", 1_000_000.00);

    // Print information about the objects you created
    printEmployee(eng);
    printEmployee(adm);
    printEmployee(mgr);
    printEmployee(mgr1);
    printEmployee(dir);

    System.out.println("\nTesting raiseSalary and setName on Manager:");
    mgr.setName ("Barbara Johnson-Smythe");
    mgr.raiseSalary(10_000.00);
    printEmployee(mgr);

}

public static void printEmployee(Employee emp) {
    System.out.println(); // Print a blank line as a separator
    // Print out the data in this Employee object
    System.out.println("Employee id:         " + emp.getEmpId());
    System.out.println("Employee name:       " + emp.getName());
    System.out.println("Employee Soc Sec #:  " + emp.getSsn());
    System.out.println("Employee salary:     " + NumberFormat.getCurrencyInstance().format((double) emp.getSalary()));
}
}

如何根据给定的问题进行编辑

Manager.java

package com.example.domain;

public class Manager extends Employee {
private String deptName;

public Manager(int empId, String name, String ssn, double salary, String deptName) {
    super(empId, name, ssn, salary);
    this.deptName = deptName;
}

public String getDeptName() {
    return deptName;
}

}

您只需添加一个数组,如下所示:

public class Manager extends Employee {
    private String deptName;
    private List<Employee> employees = new ArrayList<Employee>();

    public void addEmployee(Employee someone){
         employees.add(someone);
    }

下面是一个使用ArrayList而不是数组的示例。ArrayList适用于这种情况,因为它们是动态的(您不必设置特定的大小),并且它们内置了用于添加和删除的函数,而无需将所有现有员工上移或下移

package com.example.domain;

public class Manager extends Employee {
private String deptName;

ArrayList<Employee> employees = new ArrayList<Employee>();

public Manager(int empId, String name, String ssn, double salary, String deptName) {
super(empId, name, ssn, salary);
this.deptName = deptName;
}

public String getDeptName() {
return deptName;
}

public void add(Employee e) {
employees.add(e);

}
public void remove(Employee e) {
employees.remove(e);

}
package com.example.domain;
公共类管理器扩展了Employee{
私有字符串deptName;
ArrayList employees=新的ArrayList();
公共经理(int empId、字符串名称、字符串ssn、双薪、字符串deptName){
超级(员工ID、姓名、ssn、工资);
this.deptName=deptName;
}
公共字符串getDeptName(){
返回部门名称;
}
公共作废添加(员工e){
增加(e);
}
公共作废删除(员工e){
雇员。删除(e);
}

看起来您有一个manager类。您可以创建一个ArrayList来存储Employee类型,并使用如下代码将其删除。或者从int中删除它,您可以使用ID、名称或其他变体。希望这对您有所帮助,或者可以让您朝着正确的方向前进

`    public void removeEmployee(Employee emp, int position) {
        this.empArray.remove(position);
        System.out.println("Employee was deleted.");
    }`

   public void addEmployee(Employee emp) {
            this.empArray.add(emp);
            System.out.println("Employee was Added.");
    }

使用ArrayList获得Employee对象的列表。在将对象添加到列表之前,最好先进行null检查

package com.test;
导入java.util.ArrayList; 导入java.util.List

公共类管理器扩展了Employee{

private String deptName;

private List<Employee> empList = new ArrayList<Employee>();

public Manager(int empId, String name, String ssn, double salary,
        String deptName) {
    super(empId, name, ssn, salary);
    this.deptName = deptName;
}

public String getDeptName() {
    return deptName;
}

public void addEmployee(Employee employee) {
    if (employee != null) {
        empList.add(employee);
    }
}

public boolean removeEmployee(Employee employee) {
    return empList.remove(employee);
}
私有字符串deptName;
private List empList=new ArrayList();
公共经理(int empId、字符串名称、字符串ssn、双薪、,
字符串名称){
超级(员工ID、姓名、ssn、工资);
this.deptName=deptName;
}
公共字符串getDeptName(){
返回部门名称;
}
公共无效添加员工(员工){
if(employee!=null){
empList.add(雇员);
}
}
公共布尔removeEmployee(雇员雇员){
返回员工列表。删除(员工);
}

}

使用员工列表

ArrayList<Employee> employees = new ArrayList<Employee>();

empolyees.add(employee);
ArrayList employees=new ArrayList();
员工。添加(员工);

完全不清楚,投票决定关闭。使用java.util.List研究
ArrayList<Employee> employees = new ArrayList<Employee>();

empolyees.add(employee);