《研磨设计模式》chap14 迭代器模式(2)算工资举例
生活随笔
收集整理的這篇文章主要介紹了
《研磨设计模式》chap14 迭代器模式(2)算工资举例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個公司:數組工資
一個公司:list工資
如何統一?
1. 正常編碼
public class PayModel { private String userName;//支付工資的人員 private double pay;//支付的工資數額 }public class SalaryManager{//用數組管理 private PayModel[] pms = null;//獲取工資列表 public PayModel[] getPays(){return pms;}//計算工資 public void calcSalary(){ PayModel pm1 = new PayModel();pm1.setPay(2200);pm1.setUserName("王五");PayModel pm2 = new PayModel();pm2.setPay(3600);pm2.setUserName("趙六");pms = new PayModel[2];pms[0] = pm1;pms[1] = pm2;} }public class PayManager{//聚合對象,這里是Java的集合對象 private List list = new ArrayList();//獲取工資列表 public List getPayList(){return list;}//計算工資,其實應該有很多參數,為了演示從簡 public void calcPay(){ PayModel pm1 = new PayModel();pm1.setPay(3800);pm1.setUserName("張三");PayModel pm2 = new PayModel();pm2.setPay(5800);pm2.setUserName("李四");list.add(pm1);list.add(pm2);} }public static void main(String[] args) {//訪問集團的工資列表PayManager payManager= new PayManager();//先計算再獲取payManager.calcPay();Collection payList = payManager.getPayList();Iterator it = payList.iterator();System.out.println("集團工資列表:");while(it.hasNext()){PayModel pm = (PayModel)it.next();System.out.println(pm);}//訪問新收購公司的工資列表SalaryManager salaryManager = new SalaryManager();//先計算再獲取salaryManager.calcSalary();PayModel[] pms = salaryManager.getPays();System.out.println("新收購的公司工資列表:");for(int i=0;i<pms.length;i++){System.out.println(pms[i]);}} 2. 統一訪問接口
總結
以上是生活随笔為你收集整理的《研磨设计模式》chap14 迭代器模式(2)算工资举例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《研磨设计模式》chap14 迭代器模式
- 下一篇: 《研磨设计模式》chap14 迭代器模式