You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.1 KiB

public class Employee {
private String id;
private String name;
private String department;
private double salary;
// 全参构造方法
public Employee(String id, String name, String department, double salary) {
this.id = id;
this.name = name;
this.department = department;
setSalary(salary);
}
// getter和setter
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
if (salary >= 2000) {
this.salary = salary;
} else {
System.out.println("工资不能低于2000!");
}
}
// 涨薪方法
public void raiseSalary(double percent) {
double newSalary = salary * (1 + percent / 100);
setSalary(newSalary);
}
}
// 测试类
class TestEmployee {
public static void main(String[] args) {
Employee emp1 = new Employee("1001", "张三", "技术部", 5000);
Employee emp2 = new Employee("1002", "李四", "销售部", 3000);
System.out.println("=== 初始信息 ===");
System.out.println(emp1.getName() + " - " + emp1.getDepartment() + " - 工资:" + emp1.getSalary());
System.out.println(emp2.getName() + " - " + emp2.getDepartment() + " - 工资:" + emp2.getSalary());
System.out.println("\n=== 涨薪后 ===");
emp1.raiseSalary(10);
System.out.println(emp1.getName() + "涨薪后工资:" + emp1.getSalary());
System.out.println("\n=== 修改部门 ===");
emp2.setDepartment("市场部");
System.out.println(emp2.getName() + "新部门:" + emp2.getDepartment());
System.out.println("\n=== 测试非法工资 ===");
emp2.setSalary(1500);
}
}