编写一个java程序,定义并测试一个代表员工的Employee类。员工属性包括“编号”、“姓名”、“基本薪水”、“薪水增长额”;还包括 “计算增长后的工资总额”。
时间: 2023-06-14 08:06:23 浏览: 669
下面是一个可能的 Employee 类的实现:
```java
public class Employee {
private int id;
private String name;
private double salary;
private double salaryIncrease;
public Employee(int id, String name, double salary, double salaryIncrease) {
this.id = id;
this.name = name;
this.salary = salary;
this.salaryIncrease = salaryIncrease;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public double getSalaryIncrease() {
return salaryIncrease;
}
public double getTotalSalary() {
return salary + salaryIncrease;
}
public void setSalaryIncrease(double salaryIncrease) {
this.salaryIncrease = salaryIncrease;
}
}
```
这个类有四个属性:员工编号,姓名,基本薪水和薪水增长额。它还有一个计算增长后的工资总额的方法 getTotalSalary()。还有一个 setter 方法 setSalaryIncrease() 可以用来设置薪水增长额。
下面是一个例子,展示如何使用 Employee 类:
```java
public class Main {
public static void main(String[] args) {
Employee employee = new Employee(1, "John Smith", 50000, 5000);
System.out.println("Employee name: " + employee.getName());
System.out.println("Employee salary: " + employee.getSalary());
System.out.println("Employee salary increase: " + employee.getSalaryIncrease());
System.out.println("Employee total salary: " + employee.getTotalSalary());
employee.setSalaryIncrease(6000);
System.out.println("Employee total salary after increase: " + employee.getTotalSalary());
}
}
```
这个例子创建了一个 Employee 对象,打印出了员工的姓名,基本薪水,薪水增长额和工资总额。然后,它使用 setSalaryIncrease() 方法增加了薪水增长额,再次打印出工资总额。输出应该如下:
```
Employee name: John Smith
Employee salary: 50000.0
Employee salary increase: 5000.0
Employee total salary: 55000.0
Employee total salary after increase: 56000.0
```
阅读全文