JAVA8学习——深入浅出Lambda表达式(学习过程) lambda表达式: 我们为什么要用lambda表达式
在JAVA中,我们无法将函数作为参数传递给一个方法,也无法声明返回一个函数的方法。
在JavaScript中,函数参数是一个函数,返回值是另一个函数的情况下非常常见的,JavaScript是一门非常典型的函数式编程语言,面向对象的语言
//如,JS中的函数作为参数 a.execute(callback(event){ event... })Java匿名内部类实例
后面补充一个匿名内部类的代码实例Gradle的使用和好处
需要自行补充Gradle的学习Gradle完全可以使用Maven的所有能力
Maven基于XML的配置文件,Gradle是基于编程式配置.Gradle文件
自定义匿名内部类
public class SwingTest { public static void main(String[] args) { JFrame jFrame = new JFrame("my Frame"); JButton jButton = new JButton("My Button"); jButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { System.out.println("Button Pressed"); } }); jFrame.add(jButton); jFrame.pack(); jFrame.setVisible(true); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }改造前:
jButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { System.out.println("Button Pressed"); } });改造后:
jButton.addActionListener(actionEvent -> System.out.println("Button Pressed")); Lambda表达式的基本结构会有自动推断参数类型的功能
(pram1,pram2,pram3)->{ } 函数式接口 概念后期补(接口文档源码,注解源码) 抽象方法,抽象接口 1个接口里面只有一个抽象方法,可以有几个具体的方法 /** * 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. * * 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. * * <p>Note that instances of functional interfaces can be created with * lambda expressions, method references, or constructor references. * * <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> * * <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. * * @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 {} 关于函数式接口: 1.如何一个接口只有一个抽象方法,那么这个接口就是函数式接口 2.如果我们在某个接口上生命了FunctionalInterface注解,那么编译器就会按照函数式接口的定义来要求该注解 3.如果某个接口只有一个抽象方法,但我们没有给该接口生命FunctionalInterface接口,编译器也还会把该接口当做成一个函数是接口。(英文最后一段)通过对实例对函数式接口深入理解
对 @FunctionalInterface public interface MyInterface { void test(); } 错 @FunctionalInterface public interface MyInterface { void test(); String tostring1(); } 对 (tostring为重写Object类的方法) @FunctionalInterface public interface MyInterface { void test(); String toString(); }升级扩展,使用lambda表达式
@FunctionalInterface interface MyInterface { void test(); String toString(); } public class Test2{ public void myTest(MyInterface myInterface){ System.out.println("1"); myInterface.test(); System.out.println("2"); } public static void main(String[] args) { Test2 test2 = new Test2(); //1.默认调用接口里面的接口函数。默认调用MyTest接口里面的test方法。 //2.如果没有参数传入方法,那么可以直接使用()来表达,如下所示 test2.myTest(()-> System.out.println("mytest")); MyInterface myInterface = () -> { System.out.println("hello"); }; System.out.println(myInterface.getClass()); //查看这个类 System.out.println(myInterface.getClass().getSuperclass());//查看类的父类 System.out.println(myInterface.getClass().getInterfaces()[0]);// 查看此类实现的接口 } } 默认方法:接口里面,从1.8开始,也可以拥有方法实现了。