From 0e088f925b8a07e801653fffa206b263e6a87c58 Mon Sep 17 00:00:00 2001 From: Mengxinyao Date: Sun, 5 Apr 2026 11:16:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(w4):W4-=E5=AD=9F=E9=91=AB=E5=9E=9A-2025060?= =?UTF-8?q?10204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w3/public class Employee {.groovy | 87 +++++++++++++++++++++++++++++ w3/public class TestEmployee {.java | 17 ++++++ 2 files changed, 104 insertions(+) create mode 100644 w3/public class Employee {.groovy create mode 100644 w3/public class TestEmployee {.java diff --git a/w3/public class Employee {.groovy b/w3/public class Employee {.groovy new file mode 100644 index 0000000..972cbe2 --- /dev/null +++ b/w3/public class Employee {.groovy @@ -0,0 +1,87 @@ +public class Employee { + // 属性:工号、姓名、部门、工资 + private String id; + private String name; + private String department; + private double salary; + + // 最低工资标准(假设为2000) + private static final double MIN_SALARY = 2000.0; + + // 全参构造方法 + public Employee(String id, String name, String department, double salary) { + this.id = id; + this.name = name; + this.department = department; + this.salary = salary; + } + + // 无参构造方法(可选) + public Employee() { + } + + // 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 >= MIN_SALARY) { + this.salary = salary; + } else { + System.out.println("工资不能低于最低工资标准 " + MIN_SALARY); + } + } + + // 按百分比增加工资的方法 + public void raiseSalary(double percent) { + if (percent < 0) { + System.out.println("百分比不能为负数"); + return; + } + + double newSalary = salary * (1 + percent / 100); + // 确保增加后仍不低于最低工资 + if (newSalary < MIN_SALARY) { + this.salary = MIN_SALARY; + } else { + this.salary = newSalary; + } + } + + // 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/public class TestEmployee {.java b/w3/public class TestEmployee {.java new file mode 100644 index 0000000..84dd965 --- /dev/null +++ b/w3/public class TestEmployee {.java @@ -0,0 +1,17 @@ +public class TestEmployee { + public static void main(String[] args) { + Employee emp1 = new Employee("E001", "张三", "技术部", 5000.0); + Employee emp2 = new Employee("E002", "李四", "销售部", 1800.0); // 会自动设为 2000 + + System.out.println("初始状态:"); + System.out.println(emp1); + System.out.println(emp2); + + emp1.raiseSalary(10); // 涨 10% + emp2.raiseSalary(-5); // 无效操作 + + System.out.println("\n调整后:"); + System.out.println(emp1); + System.out.println(emp2); + } +} \ No newline at end of file