关于Java 设计模式 迭代器模式 Iterator

关于Java 设计模式 迭代器模式 Iterator

1、IAction.java

package com.ssm.cts.pattern.iterator;

public interface IAction {
  public String getActionInfo();	
}

2、Action.java

package com.ssm.cts.pattern.iterator;

public class Action implements IAction {

  // 项目名称
  private String name = "";

  // 项目费用
  private int cost = 0;

  public Action(String name, int cost) {
    super();
    this.name = name;
    this.cost = cost;
  }

  @Override
  public String getActionInfo() {
    String info = "";

    // 获得项目的名称
    info = info + "项目名称是:" + this.name + "\t 项目费用:" + this.cost;

    return info;
  }

}

3、Demo.java

package com.ssm.cts.pattern.iterator;

import java.util.ArrayList;

public class Demo {

  public static void main(String[] args) {

    ArrayList<IAction> actionList = new ArrayList<IAction>();

    actionList.add(new Action("营销项目", 1000000));
    actionList.add(new Action("促销项目", 5000000));

    for (int i = 1; i < 10; i++) {
      actionList.add(new Action("第" + i + "个项目", i * 10000));
    }

    // 遍历一下ArrayList,把所有的数据都取出
    for (IAction action : actionList) {
      System.out.println(action.getActionInfo());
    }
  }

}

 

发表回复

您的电子邮箱地址不会被公开。