From 15cb24349ee6fad9fa8cb133e143d992243b0c5a Mon Sep 17 00:00:00 2001 From: JianXinyi <1259606552@qq.com> Date: Thu, 26 Mar 2026 21:41:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'w3'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w3/Employee.java | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 w3/Employee.java diff --git a/w3/Employee.java b/w3/Employee.java new file mode 100644 index 0000000..50c1eeb --- /dev/null +++ b/w3/Employee.java @@ -0,0 +1,83 @@ +package w3; + + +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); + } +} \ No newline at end of file