请用Java写一个程序(1)编写一个程序,编写一个名字 Employee( 雇员 ) 的父类和两个名为 Manager (经理)、Technician(技术人员)的子类。 (2)Employee 类包含 name,salary 和 address 三个属性,定义一个构造方法为成员变量初始化,一个成员方法 show 用于显示这些属性值。 Manager 类有一个名为 department (部门)的属性,Technician(技术人员)Manager 类有一个名为worktime (工作时间)的属性,定义构造方法显示调用父类的构造方法为成员变量赋值,重写父类的 show 方法输出信息;创建 Manager Technician类的对象并显示其详细信息。
时间: 2024-02-21 17:01:54 浏览: 96
以下是用Java编写的程序,实现了Employee类及其两个子类Manager和Technician:
```java
public class Employee {
private String name;
private double salary;
private String address;
public Employee(String name, double salary, String address) {
this.name = name;
this.salary = salary;
this.address = address;
}
public void show() {
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
System.out.println("Address: " + address);
}
}
public class Manager extends Employee {
private String department;
public Manager(String name, double salary, String address, String department) {
super(name, salary, address);
this.department = department;
}
@Override
public void show() {
super.show();
System.out.println("Department: " + department);
}
}
public class Technician extends Employee {
private int worktime;
public Technician(String name, double salary, String address, int worktime) {
super(name, salary, address);
this.worktime = worktime;
}
@Override
public void show() {
super.show();
System.out.println("Worktime: " + worktime);
}
}
public class Main {
public static void main(String[] args) {
Manager manager = new Manager("Tom", 10000, "Beijing", "Sales");
manager.show();
Technician technician = new Technician("Jerry", 8000, "Shanghai", 8);
technician.show();
}
}
```
程序中,Employee类包含了三个属性name、salary和address,以及一个构造方法和一个成员方法show;Manager和Technician类继承了Employee类,并添加了各自的属性department和worktime,以及重写了show方法。
在Main类中,创建了一个Manager对象和一个Technician对象,并分别调用show方法输出详细信息。
阅读全文