学以致用——Java源码——使用接口及多态处理应付账款(发票及员工工资)(更简单的方法)(Accounts Payable System Modification 2)

在上一习题中,通过修改Employee的各个子类,实现了使用接口及多态处理应付账款。

参考:

学以致用——Java源码——使用接口及多态处理应付账款(发票及员工工资)(Accounts Payable System Modification),

https://blog.csdn.net/hpdlzu80100/article/details/86001126

而紧接着的这道题则告诉我们这么一个道理:很多事情,有更为优雅的解决方法!

题目描述:

10.16 (Accounts Payable System Modification) It’s possible to include the functionality of the payroll application (Figs. 10.4–10.9) in the accounts payable application without modifying Employee subclasses SalariedEmployee, HourlyEmployee, CommissionEmployee or BasePlusCommission- Employee. To do so, you can modify class Employee (Fig. 10.4) to implement interface Payable and declare method getPaymentAmount to invoke method earnings. Method getPaymentAmount would then be inherited by the subclasses in the Employee hierarchy. When getPaymentAmount is called for a particular subclass object, it polymorphically invokes the appropriate earnings method for that subclass. Reimplement Exercise 10.15 using the original Employee hierarchy from the payroll application of Figs. 10.4–10.9. Modify class Employee as described in this exercise, and do not modify any of class Employee’s subclasses.

更简单的解法,即在Employee类中,添加一个新方法:

   @Override
   public double getPaymentAmount()
   {
      return earnings();
   }

其他的子类保持不变。

完整代码就不赘述了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值