首頁  >  文章  >  Java  >  淺談JAVA8中行為參數化的演變過程

淺談JAVA8中行為參數化的演變過程

无忌哥哥
无忌哥哥原創
2018-07-19 10:05:552352瀏覽

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中行為參數化的演變過程的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn