用Java (1)定义一个Employee类:这是所有员工总的父类,属性:员工的姓名,员工的年龄。提供构造器和输出员工信息的方法。 (2)定义一个经理类Manager:Employee的子类,属性:职位、薪水。提供构造器和输出经理信息的方法,提供一个经理的管理行为方法。 (3)定义一个工人类Worker:Employee的子类,属性:部门、薪水。提供构造器和输出工人信息的方法,提供一个工人的工作方法。 (4)定义一个公共类EmployeeAPP类,创建上述的三个类的实例,并测试其中方法。
时间: 2023-06-20 08:08:26 浏览: 124
java代码-定义一个员工信息类Employee
下面是Java代码实现:
```java
// Employee类
public class Employee {
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
// 经理类Manager
public class Manager extends Employee {
private String position;
private double salary;
public Manager(String name, int age, String position, double salary) {
super(name, age);
this.position = position;
this.salary = salary;
}
public void display() {
super.display();
System.out.println("Position: " + position);
System.out.println("Salary: " + salary);
}
public void manage() {
System.out.println("I am managing.");
}
}
// 工人类Worker
public class Worker extends Employee {
private String department;
private double salary;
public Worker(String name, int age, String department, double salary) {
super(name, age);
this.department = department;
this.salary = salary;
}
public void display() {
super.display();
System.out.println("Department: " + department);
System.out.println("Salary: " + salary);
}
public void work() {
System.out.println("I am working.");
}
}
// EmployeeAPP类
public class EmployeeAPP {
public static void main(String[] args) {
Manager manager = new Manager("Tom", 35, "CEO", 10000);
manager.display();
manager.manage();
System.out.println();
Worker worker = new Worker("Jack", 25, "IT", 5000);
worker.display();
worker.work();
}
}
```
运行结果:
```
Name: Tom
Age: 35
Position: CEO
Salary: 10000.0
I am managing.
Name: Jack
Age: 25
Department: IT
Salary: 5000.0
I am working.
```
阅读全文