Java 方法引用之類名引用靜態方法案例

(1)類名引用靜態方法

/*
    通過類名引用靜態成員方法
 */
public class StaticMethodDemo {
    public static int getAbs(int num, Calcable c){
        return c.getABS(num);
    }

    public static void main(String[] args) {
        int abs = getAbs(-10, s -> Math.abs(s));
        System.out.println(abs);

        int abs1 = getAbs(1000, Math::abs);
        System.out.println(abs1);
    }
}

(2)函數式接口

@FunctionalInterface
public interface Calcable {
    //定義一個抽象方法,對整數類型的數據進行絕對值計算,並返回結果
    public abstract int getABS(int num);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章