首页 > 代码库 > Java1.8-Lambda
Java1.8-Lambda
写的很全很好的一个博客: http://blog.csdn.net/ioriogami/article/details/12782141
什么是函数接口: http://www.lambdafaq.org/what-is-a-functional-interface/ 肤浅的理解就是:函数注解的接口是这样一个类型,这个类型的作用相当于一个函数
单看这两个博客理解起来有些吃力,看看java1.8中对FounctionalInterface的解释:
/* * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.lang; import java.lang.annotation.*; /** * An informative annotation type used to indicate that an interface * type declaration is intended to be a <i>functional interface</i> as * defined by the Java Language Specification. * :functionalInterface注解用来指明一个接口类型是functional interface,这句话好像没什么用 * Conceptually, a functional interface has exactly one abstract * method. Since {@linkplain java.lang.reflect.Method#isDefault() * default methods} have an implementation, they are not abstract. If * an interface declares an abstract method overriding one of the * public methods of {@code java.lang.Object}, that also does * <em>not</em> count toward the interface‘s abstract method count * since any implementation of the interface will have an * implementation from {@code java.lang.Object} or elsewhere. *:一个函数注解借口准确的说,只有一个抽象方法,且这个方法不能是重写的Object的public方法(不计入does not count toward) * <p>Note that instances of functional interfaces can be created with * lambda expressions, method references, or constructor references. *:一个函数注解借口可以使lambda表达式、方法的引用、或者构造函数的引用 * <p>If a type is annotated with this annotation type, compilers are * required to generate an error message unless: *:使用这个注解的时候,为了使编译器不报错,至少要满足: * <ul> * <li> The type is an interface type and not an annotation type, enum, or class. * <li> The annotated type satisfies the requirements of a functional interface. * </ul> *:type是借口而不是注解、枚举、类 * <p>However, the compiler will treat any interface meeting the * definition of a functional interface as a functional interface * regardless of whether or not a {@code FunctionalInterface} * annotation is present on the interface declaration. *:这里描述没用使用functionalInterface注解却被认作是函数注解接口的情况:meeting the definition of a functional interface即满足了函数接口的定义 * @jls 4.3.2. The Class Object * @jls 9.8 Functional Interfaces * @jls 9.4.3 Interface Method Body * @since 1.8 */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface FunctionalInterface {}
简单的例子:
Runnable接口早就有了,FunctionalInterface是1.8才出的,Runnable接口只有一个抽象函数,满足FounctionalInterface的定义,属于上面提到的特殊情况,在1.8中加上了@FunctionalInterface注解
Thread th=new Thread(()->System.out.println("hello")); th.start();
Java1.8-Lambda
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。