(1)定义员⼯类Employee,包含属性员⼯ID、员⼯的姓名name,员⼯的⼯资salary (2)在(1)的基础上,完成相关的构造⽅法、相关属性的 set/get ⽅法,并⾃定义⽅法 toString,⽤于打印输出内部属性的值 (3)⾃拟数据,创建10个Employee对象,放⼊ArrayList中
时间: 2024-04-09 19:33:08 浏览: 110
java代码-定义一个员工信息类Employee,使用ArrayList对员工信息进行添加和显示。
好的,以下是根据你的要求定义的Employee类:
```java
public class Employee {
private int employeeID;
private String name;
private double salary;
public Employee(int employeeID, String name, double salary) {
this.employeeID = employeeID;
this.name = name;
this.salary = salary;
}
public int getEmployeeID() {
return employeeID;
}
public void setEmployeeID(int employeeID) {
this.employeeID = employeeID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"employeeID=" + employeeID +
", name='" + name + '\'' +
", salary=" + salary +
'}';
}
}
```
接下来,我们可以创建10个Employee对象,并将其放入ArrayList中:
```java
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Employee> employees = new ArrayList<>();
employees.add(new Employee(1, "John", 5000));
employees.add(new Employee(2, "Alice", 6000));
employees.add(new Employee(3, "Bob", 5500));
employees.add(new Employee(4, "Emily", 7000));
employees.add(new Employee(5, "David", 4500));
employees.add(new Employee(6, "Sarah", 8000));
employees.add(new Employee(7, "Michael", 6500));
employees.add(new Employee(8, "Jessica", 5500));
employees.add(new Employee(9, "Daniel", 7500));
employees.add(new Employee(10, "Olivia", 6000));
for (Employee employee : employees) {
System.out.println(employee.toString());
}
}
}
```
以上代码会创建10个Employee对象,并将其打印输出。
请注意,这只是一个示例,你可以根据自己的需求进行适当的修改。
阅读全文