zoukankan      html  css  js  c++  java
  • 方法引用的基本使用

    1、例子中用到的函数式接口

    package com.ly.demo.java8;
    
    /**
     * @author liuyang
     * @create 2020-05-17 23:16
     */
    @FunctionalInterface
    public interface IntArrayBuilder {
        int[] buildIntArray(int len);
    }
    package com.ly.demo.java8;
    
    /**
     * @author liuyang
     * @create 2020-05-17 21:57
     */
    // 函数接口,有且仅有一个抽象方法的接口
    @FunctionalInterface
    public interface MyInterface {
        void print(String str);
    }
    package com.ly.demo.java8;
    
    /**
     * @author liuyang
     * @create 2020-05-17 23:00
     */
    @FunctionalInterface
    public interface MyObjBuilder {
        MyObj build(String name,Integer age);
    }

    2、例子中用到的类

    package com.ly.demo.java8;
    
    /**
     * @author liuyang
     * @create 2020-05-17 21:57
     */
    public class MyObj extends MyObjSup {
        public String name;
        public Integer age;
    
        public MyObj() {
        }
    
        public MyObj(String name, Integer age) {
            this.name = name;
            this.age = age;
        }
    
        public void printStr(MyInterface mi, String str) {
            mi.print(str);
        }
    
        public void printUpperCase(String str) {
            System.out.println(str.toUpperCase());
        }
    
        public static void printLowerCase(String str) {
            System.out.println(str.toLowerCase());
        }
    
        @Override
        public void sayHello(String s) {
            System.out.println("MyObj-你好,我是:"+s);
        }
    
        public void say1() {
            // 引用父类的方法
            printStr(super::sayHello,"小明");
        }
    
        public void say2() {
            // 引用本类的方法
            printStr(this::sayHello,"小芳");
        }
    
        MyObj buildMyObj(MyObjBuilder myObjBuilder,String name,Integer age) {
            return myObjBuilder.build(name,age);
        }
    
        int[] createIntArray(int len,IntArrayBuilder iab) {
            return iab.buildIntArray(len);
        }
    }
    package com.ly.demo.java8;
    
    /**
     * @author liuyang
     * @create 2020-05-17 22:31
     */
    public class MyObjSup {
        public void sayHello(String s) {
            System.out.println("MyObjSup-你好,我是:"+s);
        }
    }

    3、测试类

    package com.ly.demo.java8;
    
    import org.junit.Test;
    
    /**
     * @author liuyang
     * @create 2020-05-17 22:00
     */
    public class MethodRefTest {
        @Test
        public void test1() {
            MyObj myObj = new MyObj();
            // 函数式接口都可以用Lambda表达式代替
            myObj.printStr(s -> System.out.println(s),"hello1");
        }
    
        @Test
        public void test2() {
            MyObj myObj = new MyObj();
            // Lambda表达式也可以用方法引用替代
            // 被引用方法的入参必须和函数式接口方法的入参一致
            myObj.printStr(System.out::println,"hello2");
        }
    
        @Test
        public void test3() {
            MyObj myObj = new MyObj();
            // 引用对象的实例方法
            myObj.printStr(myObj::printUpperCase,"hello3");
        }
    
        @Test
        public void test4() {
            MyObj myObj = new MyObj();
            // 引用类的静态方法
            myObj.printStr(MyObj::printLowerCase,"HELLO4");
        }
    
        @Test
        public void test5() {
            // 测试引用父类的方法
            new MyObj().say1();
        }
    
        @Test
        public void test6() {
            // 测试引用本类的方法
            new MyObj().say2();
        }
    
        @Test
        public void test7() {
            // 测试引用构造方法
            MyObj myObj = new MyObj().buildMyObj(MyObj::new,"liuyang",18);
            System.out.println(myObj.name);
            System.out.println(myObj.age);
        }
    
        @Test
        public void test8() {
            // 测试引用数组的构造方法
            int[] arr = new MyObj().createIntArray(10,int[]::new);
            System.out.println(arr.length);
        }
    
    }
  • 相关阅读:
    使用VisualStudio2015开发QT项目
    界面控件
    SmartGit 试用过期
    视图和模型变换
    模型变换和视图变换
    一元二次方程
    论cudnn与cuda之间的关系,和实际例子测试。
    在Ubuntu 18.04上安装Tensorflow
    ubuntu14.04安装CUDA8.0
    Windows10系统远程桌面连接出现卡顿如何解决
  • 原文地址:https://www.cnblogs.com/liuyang-520/p/12907723.html
Copyright © 2011-2022 走看看