浏览 2480 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-07-03
A食物 public class AFood { private String name; private double price; public AFood(String name,double price){ this.name= name; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { // TODO Auto-generated method stub return "Name:" + name + " ,Price" + price; } } A菜单 public class AMenu implements Menu { //利用List private static List<AFood> foodList = new ArrayList<AFood>(); static { foodList.add(new AFood("1food", 20.00)); foodList.add(new AFood("2food", 10.00)); foodList.add(new AFood("3food", 15.0)); } @Override public Iterator createIterator() { // TODO Auto-generated method stub return foodList.iterator(); } } B食物 public class BFood { private String name; private double price; private boolean isVaggltable; public BFood(String name, double price, boolean isVaggltable) { this.name = name; this.price = price; this.isVaggltable = isVaggltable; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public boolean isVaggltable() { return isVaggltable; } public void setVaggltable(boolean isVaggltable) { this.isVaggltable = isVaggltable; } @Override public String toString() { // TODO Auto-generated method stub return "Name:" + name + " Price:" + price + " isVaggltable:" + isVaggltable; } } B菜单 public class BMenu implements Menu { //利用数组封装 private static BFood[] BFoodArray = null; static { BFoodArray = new BFood[] { new BFood("1bfood", 63, true), new BFood("2bfood", 36, true), new BFood("3bfood", 25, false) }; } @Override public Iterator createIterator() { // TODO Auto-generated method stub ArrayIterator<BFood> ai = new ArrayIterator<BFood>(BFoodArray); return ai; } } 所有菜单接口 public interface Menu { //创建迭代器 public Iterator createIterator(); } 数组迭代器 public class ArrayIterator<T> implements Iterator { private T[] arr; private int index = -1; public ArrayIterator(T[] arr) { this.arr = arr; } @Override public boolean hasNext() { // TODO Auto-generated method stub index++; if(index>arr.length-1) return false; return arr[index] != null; } @Override public T next() { // TODO Auto-generated method stub return arr[index]; } @Override public void remove() { // TODO Auto-generated method stub throw new UnsupportedOperationException(); } } 测试类--打印所有菜单 public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Test test = new Test(); Menu a = new AMenu(); Menu b = new BMenu(); test.printMenu(a,b); } private void printMenu(Menu...menus){ for(Menu menu:menus){ Iterator iterator = menu.createIterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-07-03
代码太多,说明太少
写了给自己看吧 |
|
返回顶楼 | |
发表时间:2009-07-04
whatwhat 写道 代码太多,说明太少
写了给自己看吧 谢谢建议,以后改善! |
|
返回顶楼 | |