编写一个员工类,成员变量包括编号、姓名、年龄、工资、身份证号码、员工人数、员工工资总额。
时间: 2024-12-14 18:22:03 浏览: 6
在编写一个员工类时,我们可以考虑如下结构:
```java
public class Employee {
private int employeeID; // 编号
private String name; // 姓名
private int age; // 年龄
private double salary; // 工资
private String idNumber; // 身份证号码
// 构造函数
public Employee(int id, String name, int age, double salary, String idNumber) {
this.employeeID = id;
this.name = name;
this.age = age;
this.salary = salary;
this.idNumber = idNumber;
}
// 获取和设置方法
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 int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public double getSalary() { return salary; }
public void setSalary(double salary) { this.salary = salary; }
public String getIdNumber() { return idNumber; }
public void setIdNumber(String idNumber) { this.idNumber = idNumber; }
// 计算员工总人数和工资总额的相关方法
public static int getTotalEmployees(Employee[] employees) {
int total = employees.length;
return total;
}
public static double getTotalSalary(Employee[] employees) {
double totalSalary = 0;
for (Employee employee : employees) {
totalSalary += employee.getSalary();
}
return totalSalary;
}
}
```
在这个类中,我们有基本的成员变量,还包含了构造函数用于初始化这些属性。同时,为了方便统计所有员工的总数和工资总额,我们添加了静态方法`getTotalEmployees`和`getTotalSalary`。
阅读全文