java输出一组公司员工的信息列表,其中员工具备以下特征: 雇员:员工编号、姓名、工资; 经理:员工编号、姓名、工资、管理部门; 总监:员工编号、姓名、工资、津贴;
时间: 2023-09-17 07:13:50 浏览: 76
以下是Java代码示例:
```
public class Employee {
private int empId;
private String name;
private double salary;
public Employee(int empId, String name, double salary) {
this.empId = empId;
this.name = name;
this.salary = salary;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"empId=" + empId +
", name='" + name + '\'' +
", salary=" + salary +
'}';
}
}
public class Manager extends Employee {
private String department;
public Manager(int empId, String name, double salary, String department) {
super(empId, name, salary);
this.department = department;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public String toString() {
return "Manager{" +
"empId=" + getEmpId() +
", name='" + getName() + '\'' +
", salary=" + getSalary() +
", department='" + department + '\'' +
'}';
}
}
public class Director extends Employee {
private double allowance;
public Director(int empId, String name, double salary, double allowance) {
super(empId, name, salary);
this.allowance = allowance;
}
public double getAllowance() {
return allowance;
}
public void setAllowance(double allowance) {
this.allowance = allowance;
}
@Override
public String toString() {
return "Director{" +
"empId=" + getEmpId() +
", name='" + getName() + '\'' +
", salary=" + getSalary() +
", allowance=" + allowance +
'}';
}
}
public class EmployeeList {
public static void main(String[] args) {
Employee e1 = new Employee(1001, "John Smith", 35000);
Manager m1 = new Manager(1002, "Jane Doe", 45000, "Sales");
Director d1 = new Director(1003, "Mike Johnson", 80000, 15000);
System.out.println(e1);
System.out.println(m1);
System.out.println(d1);
}
}
```
在上面的示例中,我们定义了三个类:Employee、Manager和Director,并在EmployeeList类中创建了一个员工信息列表。我们可以看到,每个类都有自己的属性和方法,而Manager和Director类继承了Employee类的属性和方法,并添加了它们自己的属性和方法。我们还重写了每个类的toString()方法,以便在输出时可以打印出正确的信息。
运行EmployeeList类的main方法,得到以下输出:
```
Employee{empId=1001, name='John Smith', salary=35000.0}
Manager{empId=1002, name='Jane Doe', salary=45000.0, department='Sales'}
Director{empId=1003, name='Mike Johnson', salary=80000.0, allowance=15000.0}
```
这表明我们成功地创建了一组公司员工信息列表,并正确地打印出了每个员工的信息。