zoukankan      html  css  js  c++  java
  • 【JAVA8】双冒号

      
    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");
            //下面面的forEach循环和上面的循环是等价的 
            for (String a: al) {
                MyTest.printValur(a);
            }
            //下面的for each循环和上面的循环是等价的 
            al.forEach(x->{
                MyTest.printValur(x);
            });
     
            al.forEach(AcceptMethod::printValur);
            //下面的方法和上面等价的
            Consumer<String> methodParam = MyTest::printValur; //方法参数
            al.forEach(x -> methodParam.accept(x));//方法执行accept
        }
    }

      

      上面的所有方法执行玩的结果都是如下:

    print value : a
    print value : b
    print value : c
    print value : d

      在JDK8中,接口Iterable 8中默认实现了forEach方法,调用了 JDK8中增加的接口Consumer内的accept方法,执行传入的方法参数。

  • 相关阅读:
    request、bs4爬虫
    1031 查验身份证
    1029 旧键盘
    1028 人口普查
    1027 打印沙漏
    1026 程序运行时间
    1025 反转链表
    1024 科学计数法
    1022 D进制的A+B
    1021 个位数统计
  • 原文地址:https://www.cnblogs.com/itplay/p/10733433.html
Copyright © 2011-2022 走看看