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方法,执行传入的方法参数。

  • 相关阅读:
    jQuery..1..基本使用..选择
    ORZ各路神犇
    马上搞定Android平台的Wi-Fi Direct开发
    Linux环境下搭建Android开发环境
    笑谈接口回调
    AIDL通信原理
    某个Java面试题
    直接下载SpringBoot项目本地的Excel文件
    用JSP做后台管理系统
    Singleton
  • 原文地址:https://www.cnblogs.com/itplay/p/10733433.html
Copyright © 2011-2022 走看看