Method reference in Java 8

前端 未结 5 1016
梦如初夏
梦如初夏 2021-02-13 15:31
public class Car {

    private int maxSpeed;

    public Car(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

    public int getMaxSpeed() {
        return maxS         


        
5条回答
  •  终归单人心
    2021-02-13 16:22

    Let's look at Function in detail:

    Interface Function {
    
        default  Function   andThen(Function after){}
    
        R   apply(T t);
    
        default  Function   compose(Function before){}
    
        static  Function    identity();
    
    }
    

    note the R apply(T t); Applies this function to the given argument.

    Function function = Void::?????;
    Void voidInstance = null;
    function.apply(voidInstance);
    

    This doesn't make sense. You want to pass a Void so that the function of the Void is applied ?

    A few illustrative examples of what compiles as a function

    note that c->c.getMaxSpeed() and Car::getMaxSpeed are syntactically equivalent if the method is an instanceMethod. For non-static-methods the first argument is infered from the type who's method is used, and needs to be provided later (as the instance which the method will be executed on/applied on).

    public class Car {
    
        private int maxSpeed;
    
        public Car(int maxSpeed) {
            this.maxSpeed = maxSpeed;
        }
    
        public int getMaxSpeed() {
            return this.maxSpeed;
        }
        public Void setMaxSpeed() {
            this.maxSpeed = 12;
            return null;
        }
        public static int intStaticFunction(Void v) {
            return new Random().nextInt();
        }
        public static Void voidStaticFunction(Void v) {
            return null;
        }
        public static void main(String[] args) {
            final Car carX = new Car(155);
            final Car carY = new Car(140);
    
            final List cars = new ArrayList<>();
            cars.add(carX);
            cars.add(carY);
    
            cars.sort(Comparator.comparing(Car::getMaxSpeed));
            final Function function1 = c->c.getMaxSpeed();
            final Function function2 = Car::getMaxSpeed;
            final Function function3 = Car::setMaxSpeed;
            final Function function4 = n->n;
            final Function function5 = n->5;
            final Function function6 = Car::intStaticFunction;
            final Function function7 = Car::voidStaticFunction;
            final Function function8 = function1::apply;
            final Function function9 = function2::apply;
            System.out.println(function1.apply(carX));
            System.out.println(function2.apply(carX));
            System.out.println(function8.apply(carX));
            System.out.println(function9.apply(carX));
            System.out.println(function3.apply(carX));
            System.out.println(function1.apply(carX));
            System.out.println(function2.apply(carX));
            System.out.println(function8.apply(carX));
            System.out.println(function9.apply(carX));
            System.out.println();
            System.out.println(function4.apply(null));
            System.out.println(function5.apply(null));
            System.out.println(function6.apply(null));
            System.out.println(function7.apply(null));
        }
    }
    

提交回复
热议问题