函数对象

函数对象
* 一种将函数作为参数传递的独创方法 是注意到 在我们想传递的参数 即包含数据 也保包含方法,于是我们可以定义一个
* 不包含数据 只 包含方法的类,并传递该类的一个实例,事实上,一个函数通过将其放在一个对象内部而被传递,这样的对象
* 通常称为函数对象
package javabean.newTest51;

import com.sun.istack.internal.localization.NullLocalizable;
import com.sun.xml.internal.bind.AnyTypeAdapter;
import javabean.newTest51.javabean.CaseInsensitiveCompare;
import javabean.newTest51.javabean.Circle;
import javabean.newTest51.javabean.Shape;
import javabean.newTest51.javabean.Square;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;

/**
 * 2018/5/1
 * 陈东
 * 函数对象
 */
public class Test {

    public static <AnyType> boolean contains(AnyType []arr,AnyType x){

        return false;
    }

    public static double toTalArea(Collection<? extends Shape> shapes){
        int toTalArea = 0;
        for (Shape s:shapes
             ) {
            if (s!=null){
                toTalArea+=s.area();
            }

        }
        return toTalArea;
    }
    @org.junit.Test
     public void test(){
        Collection<Square> squares = new ArrayList<Square>();
        Square square1 = new Square(6.0,7.0);
        Square square2 = new Square(5,3);
        squares.add(square1);
        squares.add(square2);
        System.out.println(Test.toTalArea(squares));
     }
    /**
     * 函数对象
     * 一种将函数作为参数传递的独创方法 是注意到 在我们想传递的参数 即包含数据 也保包含方法,于是我们可以定义一个
     * 不包含数据 只 包含方法的类,并传递该类的一个实例,事实上,一个函数通过将其放在一个对象内部而被传递,这样的对象
     * 通常称为函数对象
     */
    public static <AnyType> AnyType findMax(List<AnyType> anyTypes, Comparator<? super AnyType> cmp){
        int maxindex = 0;
        for (int i=0;i<anyTypes.size();i++){
            if (cmp.compare(anyTypes.get(i),anyTypes.get(maxindex))>0){
                maxindex = i;
            }
        }
         return anyTypes.get(maxindex);

    }
    @org.junit.Test
    public void testFun(){
         List<Shape> shapes = new ArrayList<>();
         Square square = new Square(7.0,8.0);
         shapes.add(square);
         Circle circle = new Circle(8.0);
         shapes.add(circle);
         System.out.println(Test.findMax(shapes,new CaseInsensitiveCompare()).toString());
    }
    @org.junit.Test
    public void testFUn() {
        List<Square> squares = new ArrayList<>();
        Square square = new Square(7.0, 8.0);
        Square square2 = new Square(8.0, 8.0);
        Square square3 = new Square(7.0, 9.0);
        squares.add(square);
        squares.add(square2);
        squares.add(square3);
        Comparator<? super Square> cmp = new CaseInsensitiveCompare();
        System.out.println(Test.findMax(squares,cmp).toString());
    }
}

 涉及到的实体类

package javabean.newTest51.javabean;

import java.util.Comparator;

public class CaseInsensitiveCompare implements Comparator<Shape> {
   @Override
    public int compare(Shape shape1,Shape shape2){
       if (shape2.area() < shape1.area()){
           return 1;
       }
       return 0;
   }
}


package javabean.newTest51.javabean;

public class Circle extends Shape{
    private double circlelength;

    public double getCirclelength() {
        return circlelength;
    }

    public void setCirclelength(double circlelength) {
        this.circlelength = circlelength;
    }
    public Circle(Double d){
        this.circlelength = d;
    }
    @Override
    public Double  area(){
        return 3.14*2*circlelength;
    };

    @Override
    public String toString() {
        return "Cirle: banjing"+getCirclelength();
    }
}


package javabean.newTest51.javabean;

public abstract class Shape {
    public  abstract Double area();

}



package javabean.newTest51.javabean;

public class Square extends Shape {
    private double lenhth;
    private double hehth;

    public void setLenhth(double lenhth) {
        this.lenhth = lenhth;
    }

    public double getLenhth() {
        return lenhth;
    }

    public void setHehth(double hehth) {
        this.hehth = hehth;
    }

    public double getHehth() {
        return hehth;
    }
    @Override
    public String toString() {
        return "Squre: heth:"+getHehth()+"le:"+getLenhth();
    }

    public Square(double lenhth,double hehth){
        this.hehth =hehth;
        this.lenhth = lenhth;
    }
    @Override
    public Double area(){
        return hehth*lenhth;
    };

}

 

posted @ 2018-05-01 14:58  陈东的博客  阅读(405)  评论(0编辑  收藏  举报