欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  Java

浅谈JAVA8中行为参数化的演变过程

程序员文章站 2022-04-25 11:05:35
...

1.行为参数化,就是一个方法接受多个不同的行为作为参数,并在内部使用它们,完成不同行为的能力

2.行为参数化可以让代码个好的适应不断变化的要求,减轻工作量

3.lambda表达式是这一应用更加简便

4.掌握分析谓词,定义出合适的接口和实现方法

 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();
    }

以上就是浅谈JAVA8中行为参数化的演变过程的详细内容,更多请关注其它相关文章!