function object

Function object 
* An original way to pass functions as parameters is to notice that the parameters we want to pass contain both data and methods, so we can define a
* class that contains no data but only methods, and pass the class's An instance, in fact, a function is passed by placing it inside an object, such an object
* is often called a function object
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
 * Chen Dong
 * function object
 */
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));
     }
    /**
     * function object
     * An original way to pass functions as parameters is to notice that the parameters we want to pass contain both data and methods, so we can define a
     * A class that contains no data but only methods, and is passed an instance of that class, in fact, a function is passed by putting it inside an object, such an object
     * Often called a function object
     */
    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());
    }
}

 Entity classes involved

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

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325128745&siteId=291194637