[Java 8] ::

public class AcceptMethod {
 
    public static void  printValur(String str){
        System.out.println("print value : "+str);
    }
 
    public static void main(String[] args) {
        List<String> al = Arrays.asList("a","b","c","d");
        for (String a: al) {
            AcceptMethod.printValur(a);
        }

    
        //下面的for each循環和上面的循環是等價的 
        al.forEach(x->{
            AcceptMethod.printValur(x);
        });

    }
    
}
public class MyTest {
    public static void  printValur(String str){
        System.out.println("print value : "+str);
    }
 
    public static void main(String[] args) {
        List<String> al = Arrays.asList("a", "b", "c", "d");
        al.forEach(AcceptMethod::printValur);
        //下面的方法和上面等價的
        Consumer<String> methodParam = AcceptMethod::printValur; //方法參數
        al.forEach(x -> methodParam.accept(x));//方法執行accept
    }
}

 

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