java实现员工信息添加
时间: 2023-06-02 10:05:44 浏览: 104
好的,很高兴为您解答。实现员工信息添加可以使用Java语言的面向对象编程特性,利用类、对象、继承、封装等概念进行设计和实现。以下是一个简单的员工信息添加的示例代码:
```
class Employee {
private int id; // 员工编号
private String name; // 员工姓名
private String gender; // 员工性别
private double salary; // 员工薪资
// 构造函数
public Employee(int id, String name, String gender, double salary) {
this.id = id;
this.name = name;
this.gender = gender;
this.salary = salary;
}
// getter和setter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
// toString方法
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", salary=" + salary +
'}';
}
}
class EmployeeManager {
private List<Employee> employees; // 员工列表
// 构造函数
public EmployeeManager() {
this.employees = new ArrayList<>();
}
// 添加员工
public void addEmployee(Employee employee) {
employees.add(employee);
}
// 根据ID查找员工
public Employee findEmployeeById(int id) {
for (Employee employee : employees) {
if (employee.getId() == id) {
return employee;
}
}
return null;
}
}
// 测试代码
public class Test {
public static void main(String[] args) {
EmployeeManager manager = new EmployeeManager();
// 添加员工
manager.addEmployee(new Employee(1, "Tom", "Male", 5000.0));
manager.addEmployee(new Employee(2, "Lucy", "Female", 6000.0));
// 查找员工
Employee employee = manager.findEmployeeById(2);
System.out.println(employee);
}
}
```
以上是一个简单的员工信息添加示例。在实际应用中,可能需要根据实际需求进行更加复杂的设计和实现。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)