Method reference_reference static method by class name

Reference static method
by class name: Reference static member method by class name
Class already exists, static member method already exists, static member method
can be directly referenced by class name
Example:
functional interface

package demo06.StaticMethodReference;

@FunctionalInterface
public interface Cabcable {
    
    
    //定义一个抽象方法,传递一个整数,对整数进行绝对值计算并返回
    int calaAbs(int number);
}

public class Demo01StaticMethodReference {
    
    
    //定义一个方法,方法的参数传递要计算绝对值的整数,和函数式接口Calcable
    public static int method(int number,Cabcable c){
    
    
        return c.calaAbs(number);
    }

    public static void main(String[] args) {
    
    
        //调用method方法,传递计算绝对值的整数,和Lambda表达式
        int number = method(-10, (n) -> {
    
    
            //对参数进行绝对值计算并返回结果
            return Math.abs(n);
        });
        System.out.println(number);

        int i = method(-23, Math::abs);
        System.out.println(i);
    }
}

In this example, the following two ways of writing are equivalent:
Lambda expression: n -> Math.abs(n)
Method reference: Math::abs

Program demonstration:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44664432/article/details/109163294