From 1c4deb7303ae4cbe3c719ca69b59042d95be03d6 Mon Sep 17 00:00:00 2001 From: wangyandi <3512851994@qq.com> Date: Wed, 25 Mar 2026 22:06:16 +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 | 95 ++++++++++++++++++++++++++++++++++++++++++++ w3/EmployeeTest.java | 34 ++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 w3/Employee.java create mode 100644 w3/EmployeeTest.java diff --git a/w3/Employee.java b/w3/Employee.java new file mode 100644 index 0000000..7c4fe6f --- /dev/null +++ b/w3/Employee.java @@ -0,0 +1,95 @@ +public class Employee { + // 1. 定义私有属性 + private String id; + private String name; + private String department; + private double salary; + + // 最低工资标准,设为常量 + private static final double MIN_SALARY = 2000.0; + + /** + * 2. 提供必要的构造方法 + * 这里提供一个全参构造方法 + */ + public Employee(String id, String name, String department, double salary) { + this.id = id; + this.name = name; + this.department = department; + // 使用 setSalary 方法来确保初始工资符合标准 + this.setSalary(salary); + } + + // 3. 提供合理的 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; + } + + /** + * 在 setSalary 中检查工资必须大于等于最低工资标准 + */ + public void setSalary(double salary) { + if (salary >= MIN_SALARY) { + this.salary = salary; + } else { + System.out.println("工资 " + salary + " 低于最低标准 " + MIN_SALARY + ",已自动设置为最低标准。"); + this.salary = MIN_SALARY; + } + } + + /** + * 4. 添加一个 raiseSalary 方法,按照百分比增加工资 + * percent 为 5 表示增加 5% + */ + public void raiseSalary(double percent) { + if (percent < 0) { + System.out.println("增长率不能为负数,增长无效。"); + return; + } + double raiseAmount = this.salary * (percent / 100.0); + this.salary += raiseAmount; + // 确保增加后仍不低于最低工资 + if (this.salary < MIN_SALARY) { + this.salary = MIN_SALARY; + } + System.out.println("工资已按 " + percent + "% 的比例增长。"); + } + + /** + * 为了方便打印对象信息,重写 toString 方法 + */ + @Override + public String toString() { + return "Employee{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", department='" + department + '\'' + + ", salary=" + salary + + '}'; + } +} \ No newline at end of file diff --git a/w3/EmployeeTest.java b/w3/EmployeeTest.java new file mode 100644 index 0000000..d6e3fee --- /dev/null +++ b/w3/EmployeeTest.java @@ -0,0 +1,34 @@ +public class EmployeeTest { + public static void main(String[] args) { + // 5. 编写测试类,创建两个 Employee 对象 + + // 创建第一个员工对象,工资符合标准 + Employee emp1 = new Employee("E001", "张三", "研发部", 5000.0); + System.out.println("创建员工 emp1: " + emp1); + + // 创建第二个员工对象,工资低于最低标准 + Employee emp2 = new Employee("E002", "李四", "市场部", 1500.0); + System.out.println("创建员工 emp2: " + emp2); + + System.out.println("\n--- 开始演示属性访问和修改 ---"); + + // 演示获取属性 + System.out.println("emp1 的姓名是: " + emp1.getName()); + System.out.println("emp2 的当前工资是: " + emp2.getSalary()); + + // 演示 raiseSalary 方法 + System.out.println("\n对 emp1 进行调薪..."); + emp1.raiseSalary(10.0); + System.out.println("emp1 调薪后的信息: " + emp1); + + // 演示设置属性 + System.out.println("\n修改 emp2 的部门..."); + emp2.setDepartment("销售部"); + System.out.println("emp2 修改部门后的信息: " + emp2); + + // 演示 setSalary 的检查功能 + System.out.println("\n尝试将 emp1 的工资设置为 1800..."); + emp1.setSalary(1800); + System.out.println("emp1 修改工资后的信息: " + emp1); + } +}