Java1.8-Lambda

写的很全很好的一个博客: http://blog.csdn.net/ioriogami/article/details/12782141

 

什么是函数接口: http://www.lambdafaq.org/what-is-a-functional-interface/ 肤浅的理解就是:函数注解的接口是这样一个类型,这个类型的作用相当于一个函数;这个接口定义切且只定义了一个抽象方法(实例方法),这个方法不可以和object方法重复。

单看这两个博客理解起来有些吃力,看看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即满足了函数接口的定义;
* 也就是说,FunctionalInterface注解其实可以省略不写,感觉好像被别人耍了一样,FunctionalInterface就是个概念 * @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();

 有了上面的理解后,可以自己写一个Lambda的例子:运行结果是class java.lang.String

package com.lambda;

import java.util.concurrent.Callable;

public class HelloLambda {

    public void function(Callable callable){
        Object result=null;
        try {
// 这里使用的callable并没有单独开辟线程进行执行。开辟新线程执行需要使用Thead 的 start方法。 result
= callable.call(); System.out.println(result.getClass().toString()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { HelloLambda hello=new HelloLambda(); hello.function(()->new String("hello")); } }

 

转载于:https://www.cnblogs.com/afraidToForget/p/6595970.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值