第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > commons的Closure——高琪JAVA300讲笔记之commons

commons的Closure——高琪JAVA300讲笔记之commons

时间:2019-10-13 13:07:39

相关推荐

commons的Closure——高琪JAVA300讲笔记之commons

案例一:

其中,Employee类就是与上一篇的相同,这里不重复贴了。

还用到了一个Goods类:

1 package com.mons; 2 3 public class Goods { 4private String name; 5private double price; 6//折扣 7private boolean discount; 8//无参构造器 9public Goods() {10 super();11}12//有参构造器13public Goods(String name, double price, boolean discount) {14 super();15 this.name = name;16 this.price = price;17 this.discount = discount;18}19//setter和getter方法20public String getName() {21 return name;22}23public void setName(String name) {24 this.name = name;25}26public double getPrice() {27 return price;28}29public void setPrice(double price) {30 this.price = price;31}32public boolean isDiscount() {33 return discount;34}35public void setDiscount(boolean discount) {36 this.discount = discount;37}38@Override39public String toString() {40 return "(商品:"+this.name+",价格:"+this.price+",是否打折:"+(discount?"是":"否")+")";41}4243 }

最后是主函数:

1 package com.mons; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 7 import mons.collections4.Closure; 8 import mons.collections4.CollectionUtils; 9 import mons.collections4.Predicate; 10 import mons.collections4.functors.ChainedClosure; 11 import mons.collections4.functors.IfClosure; 12 import mons.collections4.functors.WhileClosure; 13 14 /** 15 * 函数式编程Closure 闭包 封装特定的业务功能 16 * 1、Closure 17 * 2、ifClosure IfClosure.ifClosure(断言,功能1,功能2) 18 * 3、whileClosure WhileClosure.whileClosure(断言,功能,标识) 19 * 4、ChainedClosure.chainedClosure(功能列表) 20 * 21 * CollectionUtils.forAllDo(容器,功能类对象); 22 * 23 */ 24 public class Demo03 { 25public static void main(String[] args) { 26 basic(); 27 System.out.println("============="); 28 ifClosure(); 29 System.out.println("============="); 30 whileClosure(); 31 System.out.println("============="); 32 chainClosure(); 33} 3435/** 36* 折上折 先打折商品,进行9折,满百再减20 37*/ 38public static void chainClosure() { 39 List<Goods> goodList = new ArrayList<Goods>(); 40 goodList.add(new Goods("javase视频",120,true)); 41 goodList.add(new Goods("javaee视频",100,false)); 42 goodList.add(new Goods("高新技术视频",80,false)); 4344 //满百减20 45 Closure<Goods> substract = new Closure<Goods>() { 46 @Override 47 public void execute(Goods goods) { 48 if(goods.getPrice() >= 100) { 49 goods.setPrice(goods.getPrice()-20); 50 } 51 } 52 }; 53 //打折 54 Closure<Goods> discount = new Closure<Goods>() { 55 @Override 56 public void execute(Goods goods) { 57 if(goods.isDiscount()) { 58 goods.setPrice(goods.getPrice()*0.9); 59 } 60 } 61 }; 626364 //链式操作 65 Closure<Goods> chainClo = ChainedClosure.chainedClosure(discount,substract); 6667 //关联 68 CollectionUtils.forAllDo(goodList, chainClo); 6970 //查看操作后的数据 71 for(Goods temp:goodList) { 72 System.out.println(temp); 73 } 747576} 7778/** 79* 确保所有的员工工资都大于10000,如果已经超过的不再上涨 80*/ 81public static void whileClosure() { 82 //数据 83 List<Employee> empList = new ArrayList<Employee>(); 84 empList.add(new Employee("bjsxt",20000)); 85 empList.add(new Employee("is",10000)); 86 empList.add(new Employee("good",5000)); 8788 //业务功能 每次上涨0.2 89 Closure<Employee> cols = new Closure<Employee>() { 90 91 @Override 92 public void execute(Employee emp) { 93 emp.setSalary(emp.getSalary()*1.2); 94 95 } 96 97 }; 9899 //判断100 Predicate<Employee> empPre = new Predicate<Employee>() {101 @Override102 public boolean evaluate(Employee emp) {103 return emp.getSalary()<10000;104 }105 };106 //第三个参数 false 表示while结构 先判断后执行 true do..while 先执行后判断107 Closure<Employee> whileCols = WhileClosure.whileClosure(empPre, cols, false);108 109 //工具类110 CollectionUtils.forAllDo(empList, whileCols);111 112 //操作后的数据113 Iterator<Employee> empIt = empList.iterator();114 while(empIt.hasNext()) {115 System.out.println(empIt.next());116 }117}118119120/** 121* 二选一 如果是打折商品,进行9折,否则满百减2*/123public static void ifClosure() {124 List<Goods> goodList = new ArrayList<Goods>();125 goodList.add(new Goods("javase视频",120,true));126 goodList.add(new Goods("javaee视频",100,false));127 goodList.add(new Goods("高新技术视频",80,false));128 129 //满百减0 Closure<Goods> substract = new Closure<Goods>() {131 @Override132 public void execute(Goods goods) {133 if(goods.getPrice() >= 100) {134 goods.setPrice(goods.getPrice()-20);135 }136 }137 };138 //打折139 Closure<Goods> discount = new Closure<Goods>() {140 @Override141 public void execute(Goods goods) {142 if(goods.isDiscount()) {143 goods.setPrice(goods.getPrice()*0.9);144 }145 }146 };147 148 //判断149 Predicate<Goods> pre = new Predicate<Goods>() {150 @Override151 public boolean evaluate(Goods goods) {152 return goods.isDiscount();153 }154 };155 156 //二选一157 Closure<Goods> ifClo = IfClosure.ifClosure(pre,discount,substract);158 159 //关联160 CollectionUtils.forAllDo(goodList, ifClo);161 162 //查看操作后的数据163 for(Goods temp:goodList) {164 System.out.println(temp);165 }166 167 168}169170171/**172* 基本操作173*/174public static void basic() {175 //数据176 List<Employee> empList = new ArrayList<Employee>();177 empList.add(new Employee("bjsxt",20000));178 empList.add(new Employee("is",10000));179 empList.add(new Employee("good",5000));180 181 //业务功能182 Closure<Employee> cols = new Closure<Employee>() {183 184 @Override185 public void execute(Employee emp) {186 emp.setSalary(emp.getSalary()*1.2);187 188 }189 190 };191 192 //工具类193 CollectionUtils.forAllDo(empList, cols);194 195 //操作后的数据196 Iterator<Employee> empIt = empList.iterator();197 while(empIt.hasNext()) {198 System.out.println(empIt.next());199 }200 201}03 }

运行结果:

(码农:bjsxt,敲砖钱:24000.0)(码农:is,敲砖钱:12000.0)(码农:good,敲砖钱:6000.0)=============(商品:javase视频,价格:108.0,是否打折:是)(商品:javaee视频,价格:80.0,是否打折:否)(商品:高新技术视频,价格:80.0,是否打折:否)=============(码农:bjsxt,敲砖钱:20000.0)(码农:is,敲砖钱:10000.0)(码农:good,敲砖钱:10368.0)=============(商品:javase视频,价格:88.0,是否打折:是)(商品:javaee视频,价格:80.0,是否打折:否)(商品:高新技术视频,价格:80.0,是否打折:否)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。