Java 要求定义父类Employee为抽象类,showMessage为抽象方法。增加一个子类DevelopManager扩充了:奖金prize和加班时间overtime两个属性。测试类main方法中,在创建SaleManager对象和DevelopManager对象后,通过定义Employee引用SaleManager对象和DevelopManager对象,分别调用showMessage方法输出: 销售经理信息: 员工信息:姓名:张三;id号:10001;薪金:10000元; 扩展信息:销售收入:1000000元,销售利润:100000元。 开发经理信息: 员工信息:姓名:李四;id号:20001;薪金:20000元; 扩展信息:奖金:1000元,加班时间:10小时。
时间: 2024-03-13 12:47:05 浏览: 56
Java面向对象实验指导书抽象类接口多态[参考].pdf
好的,根据您的要求,以下是Java代码实现:
```java
// 定义抽象类Employee
abstract class Employee {
private String name; // 姓名
private String id; // id号
private double salary; // 薪水
// 构造方法
public Employee(String name, String id, double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
// 抽象方法
public abstract void showMessage();
// getter和setter方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
// 定义子类DevelopManager
class DevelopManager extends Employee {
private double prize; // 奖金
private double overtime; // 加班时间
// 构造方法
public DevelopManager(String name, String id, double salary, double prize, double overtime) {
super(name, id, salary);
this.prize = prize;
this.overtime = overtime;
}
// 重写父类的showMessage方法
public void showMessage() {
System.out.println("开发经理信息:");
System.out.println("员工信息:姓名:" + getName() + ";id号:" + getId() + ";薪金:" + getSalary() + "元;");
System.out.println("扩展信息:奖金:" + prize + "元,加班时间:" + overtime + "小时。");
}
// getter和setter方法
public double getPrize() {
return prize;
}
public void setPrize(double prize) {
this.prize = prize;
}
public double getOvertime() {
return overtime;
}
public void setOvertime(double overtime) {
this.overtime = overtime;
}
}
// 测试类Main方法
public class Main {
public static void main(String[] args) {
// 创建SaleManager对象和DevelopManager对象
SaleManager s = new SaleManager("张三", "10001", 10000, 1000000, 100000);
DevelopManager d = new DevelopManager("李四", "20001", 20000, 1000, 10);
// 通过定义Employee引用SaleManager对象和DevelopManager对象,分别调用showMessage方法输出信息
Employee e1 = s;
Employee e2 = d;
e1.showMessage();
e2.showMessage();
}
}
// 定义子类SaleManager
class SaleManager extends Employee {
private double income; // 销售收入
private double profit; // 销售利润
// 构造方法
public SaleManager(String name, String id, double salary, double income, double profit) {
super(name, id, salary);
this.income = income;
this.profit = profit;
}
// 重写父类的showMessage方法
public void showMessage() {
System.out.println("销售经理信息:");
System.out.println("员工信息:姓名:" + getName() + ";id号:" + getId() + ";薪金:" + getSalary() + "元;");
System.out.println("扩展信息:销售收入:" + income + "元,销售利润:" + profit + "元。");
}
// getter和setter方法
public double getIncome() {
return income;
}
public void setIncome(double income) {
this.income = income;
}
public double getProfit() {
return profit;
}
public void setProfit(double profit) {
this.profit = profit;
}
}
```
阅读全文