以下一段代碼請看java
@Test public void test() throws Exception { List<Apple> list = new ArrayList<>(); for (Apple apple : list) { if (apple.getWeight() > 1000){ System.out.println("..."); } } }
@Test public void test() throws Exception { List<Apple> list = new ArrayList<>(); for (Apple apple : list) { if ("red".equals(apple.getColor())){ System.out.println("..."); } } }
行爲參數化編程
public void getAppleByColor(List<Apple> apples,String color){ for (Apple apple : apples) { if (color.equals(apple.getColor())){ System.out.println("..."); } } } //getAppleByColor(lists,"red"); //調用
public void getAppleByColor(List<Apple> apples,String color,Integer weight,boolean isColor){ for (Apple apple : apples) { if ((isColor && color.equals(apple.getColor())) || !isColor && weight > apple.getWeight()){ System.out.println("..."); } } } getAppleByColor(lists,"red",0,true); //調用 getAppleByColor(lists,"",123,false); //調用