private static Map<String, Consumer> maps = new HashMap<String, Consumer>();
static { //这里用到landa表达式,新特性。 其中 Cat,Dog 可以看成 if-else 中的条件 maps.put("type1", productVO -> test1(productVO)); maps.put("type2", productVO -> test2(productVO)); }
public static void main(String[] args) { Consumer cat = maps.get("Cat"); ProductVO productVO = new ProductVO(); productVO.setBrandName("sasdfs"); cat.accept(productVO); }
private static Boolean test1(ProductVO productVO) { System.out.println("RunCase2" + productVO.getBrandName()); return true; }
private static Boolean test2(ProductVO productVO) { System.out.println("RunCase1" + productVO.getBrandName()); return false; }