Maison >Java >javaDidacticiel >Une brève discussion sur l'évolution du paramétrage comportemental dans JAVA8
1. Le paramétrage du comportement est la capacité d'une méthode à accepter plusieurs comportements différents comme paramètres et à les utiliser en interne pour compléter différents comportements
2 Le paramétrage du comportement permet de personnaliser le code. changer les exigences et réduire la charge de travail
3. Les expressions Lambda facilitent cette application
4. Maîtriser les prédicats analytiques et définir les interfaces et les méthodes de mise en œuvre appropriées
public static class Apple{ private String color; private Integer weight; private String sm; public String getSm() { return sm; } public void setSm(String sm) { this.sm = sm; } public Apple(String color, int weight) { this.color = color; this.weight = weight; } @Override public String toString() { return "Apple{" + "color='" + color + '\'' +", weight=" + weight +", sm='" + sm + '\'' +'}'; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Integer getWeight() { return weight; } public void setWeight(Integer weight) { this.weight = weight; } } //从列表中筛选出绿色的苹果 public static List<Apple> filterGreenApple(List<Apple>inventory){ List<Apple>result=new ArrayList<>(); for (Apple apple : inventory) { if ("green".equals(apple.getColor())) { result.add(apple); } } return result; } //从列表中根据参数筛选出绿色的苹果 public static List<Apple>filerAppleByColor(List<Apple>appleList,String color){ List<Apple> apples=new ArrayList<>(); for (Apple apple : appleList) { if (apple.getColor().equals(color)) { apples.add(apple); } } return apples; } //统一定义行为参数接口类,这个行为的主体是apple public interface ApplePredicate{ boolean test(Apple apple); } public interface PredicateFormat{ String accept(Apple apple); } //定义泛型类的行为参数接口类,这个行为的主体不在局限某一个实物 public interface AbstratPredicate<T>{ boolean test(T t); } //参数行为化多实现类写法,实现按重量和颜色挑选苹果 public static class filterGreenWeightApple implements ApplePredicate{ @Override public boolean test(Apple apple) { return apple.getColor().equals("green")&&apple.getWeight()>100; } } public static class filteFannyApple implements PredicateFormat{ @Override public String accept(Apple apple) { String ss= apple.getWeight()>100? "light":"heavy"; return "A"+ ss+apple.getColor()+"Apple"; } } public static List<Apple>filterApplePredicate(List<Apple> appleList,ApplePredicate p){ List<Apple> apples=new ArrayList<>(); for (Apple apple : appleList) { if (p.test(apple)) { apples.add(apple); } } return apples; } public static List<Apple>filterFannyApple(List<Apple>appleList,PredicateFormat p){ List<Apple>apples=new ArrayList<>(); for (Apple apple : appleList) { apple.setSm(p.accept(apple)); apples.add(apple); } return apples; } //集成泛型接口的泛型类型方法 public static <T> List<T> filter(List<T>list,AbstratPredicate<T> pa){ List<T>lists=new ArrayList<>(); for (T t : list) { if (pa.test(t)) { lists.add(t); } } return lists; } //匿名类的笨重感 public static class MeaningOfThis{ public final int value=4; public void doIt(){ int value=6; Runnable r=new Runnable() { public final int value=5; @Override public void run() { int value=10; System.out.println(this.value); } }; r.run(); } } public static void main(String[] args) { List<Apple> appleList=Arrays.asList(new Apple("yellow",150),new Apple("green",150),new Apple("green",100)); //过滤绿色的苹果 List<Apple>result=filterGreenApple(appleList); result.stream().forEach((Apple a)->System.out.println(a)); //根据颜色参数过滤苹果 List<Apple>colorResult=filerAppleByColor(appleList,"yellow"); colorResult.stream().forEach(c->System.out.println(c)); //参数行为化多实现类写法,实现按重量和颜色挑选苹果 List<Apple>colorWeightApple=filterApplePredicate(appleList,new filterGreenWeightApple() ); colorWeightApple.stream().forEach(cw->System.out.println(cw)); List<Apple>fannyApple=filterFannyApple(appleList,new filteFannyApple()); fannyApple.stream().forEach(f->System.out.println(f)); System.out.println("-----------------------------"); //参数行为化匿名类实现 List<Apple>niming=filterApplePredicate(appleList, new ApplePredicate() { @Override public boolean test(Apple apple) { return apple.getColor().equals("green"); } }); niming.stream().forEach(n->System.out.println(n)); //匿名类的笨重感 MeaningOfThis mo=new MeaningOfThis(); mo.doIt(); //lambda表达式改写 System.out.println("-------我是lambda---------"); List<Apple>lamApples=filterApplePredicate(appleList,(Apple a)->a.getWeight()>100); lamApples.stream().forEach(la->System.out.println(la)); System.out.println("---------------"); List<Apple>lamApples1= filterFannyApple(appleList,(Apple a)->{ String ss=a.getWeight()>100?"lighter":"heavyer"; return "A"+ss+a.getColor()+"Apple"; }); lamApples1.sort((Apple a,Apple a1)->{ if (!a1.getWeight().equals(a.getWeight())) { return a1.getWeight().compareTo(a.getWeight()); }else { return a1.getColor().compareTo(a.getColor()); } }); lamApples1.stream().forEach(la1->System.out.println(la1)); //集成泛型接口的泛型类型方法 List<Integer>nums= Arrays.asList(1,2,3,4,5,6); List<Integer>numlist=filter(nums,(Integer i)->i%2==0); numlist.sort((Integer a,Integer a1)->a.compareTo(a1)); numlist.stream().forEach(i->System.out.println(i)); //自带的排序行为参数化的排序 Thread t=new Thread(()->System.out.println(new Apple("red",100))); t.start(); }
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!