用Java代码写一个员工信息类的创建,员工分为普通员工、销售员、部门经理3种角色,所有员工的
时间: 2024-05-14 14:17:08 浏览: 97
简单的java公司员工类
基本信息包括:姓名、年龄、性别、工号、工资。其中销售员还包括销售额,部门经理还包括部门名称。
以下是Java代码实现:
```
public abstract class Employee {
private String name;
private int age;
private String gender;
private String id;
private double salary;
public Employee(String name, int age, String gender, String id, double salary) {
this.name = name;
this.age = age;
this.gender = gender;
this.id = id;
this.salary = salary;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getId() {
return id;
}
public double getSalary() {
return salary;
}
public abstract double getAnnualSalary();
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", id='" + id + '\'' +
", salary=" + salary +
'}';
}
}
public class Salesman extends Employee {
private double sales;
public Salesman(String name, int age, String gender, String id, double salary, double sales) {
super(name, age, gender, id, salary);
this.sales = sales;
}
public double getSales() {
return sales;
}
public void setSales(double sales) {
this.sales = sales;
}
@Override
public double getAnnualSalary() {
return getSalary() + sales * 0.1;
}
@Override
public String toString() {
return "Salesman{" +
"name='" + getName() + '\'' +
", age=" + getAge() +
", gender='" + getGender() + '\'' +
", id='" + getId() + '\'' +
", salary=" + getSalary() +
", sales=" + sales +
'}';
}
}
public class Manager extends Employee {
private String department;
public Manager(String name, int age, String gender, String id, double salary, String department) {
super(name, age, gender, id, salary);
this.department = department;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public double getAnnualSalary() {
return getSalary() + 10000;
}
@Override
public String toString() {
return "Manager{" +
"name='" + getName() + '\'' +
", age=" + getAge() +
", gender='" + getGender() + '\'' +
", id='" + getId() + '\'' +
", salary=" + getSalary() +
", department='" + department + '\'' +
'}';
}
}
```
这里我们定义了一个抽象类 `Employee`,包含了所有员工的基本信息和一个抽象方法 `getAnnualSalary()`,表示计算年薪的方法。然后我们定义了两个具体的员工类 `Salesman` 和 `Manager`,分别继承自 `Employee`,并实现了 `getAnnualSalary()` 方法。其中 `Salesman` 类还包含了销售额这一属性,`Manager` 类包含了部门名称这一属性。
这样,我们就可以创建普通员工、销售员、部门经理这三种角色的员工信息了。例如:
```
Employee employee = new Employee("张三", 25, "男", "1001", 5000);
System.out.println(employee);
Salesman salesman = new Salesman("李四", 30, "女", "1002", 5000, 100000);
System.out.println(salesman);
System.out.println("销售员 " + salesman.getName() + " 的年薪为:" + salesman.getAnnualSalary());
Manager manager = new Manager("王五", 35, "男", "1003", 10000, "销售部");
System.out.println(manager);
System.out.println("部门经理 " + manager.getName() + " 的年薪为:" + manager.getAnnualSalary());
```
输出结果如下:
```
Employee{name='张三', age=25, gender='男', id='1001', salary=5000.0}
Salesman{name='李四', age=30, gender='女', id='1002', salary=5000.0, sales=100000.0}
销售员 李四 的年薪为:15000.0
Manager{name='王五', age=35, gender='男', id='1003', salary=10000.0, department='销售部'}
部门经理 王五 的年薪为:20000.0
```
阅读全文