5. 类型推断和变量引用

作者: HmilyMing | 来源:发表于2019-01-28 13:10 被阅读4次

    1. 类型推断

    我们之前说过 lambda 表达式是一个匿名函数,最终是返回一个实现指定接口的对象,所以你要告诉它,究竟要实现哪个接口,否则就会报错,这就是类型推断。

    如下演示几种方式告诉 lambda 表达式要实现什么接口:

    创建测试的接口

    @FunctionalInterface
    interface IMath {
        int add(int x, int y);
    }
    

    在列出 3 种调用方式

    public class TypeDemo {
    
        public static void main(String[] args) {
    //        变量类型定义
            IMath lambda = (x, y) -> x + y;
    
    //        数组里
            IMath[] lambdas = {((x, y) -> x + y)};
    
    //        强转
            Object lambda2 = (IMath)(x, y) -> x + y;
    
    //        通过返回类型
            IMath lambda3 = createLambda();
    
    //        常用这种方式,其实和 变量类型定义是一回事
            TypeDemo typeDemo = new TypeDemo();
            typeDemo.test((x, y) -> x + y);
        }
    
        public static IMath createLambda(){
            return (x, y) -> x + y;
        }
    
        public void test(IMath math){
    
        }
    }
    

    如上就是类型推断的三种使用方式。

    接下来再加一下代码,当有方法重载时要注意的地方

    继续加一个接口

    @FunctionalInterface
    interface IMath2 {
        int sub(int x, int y);
    }
    

    再加上一个 test 的重载方法

    public void test(IMath2 math){
    
    }
    

    这时候,你会发现

    typeDemo.test((x, y) -> x + y);
    

    是报错的,这时候就是有歧义了,要使用强转对应的接口来解决

    //        当有 二义性的时候,使用强转对应的接口来解决
            typeDemo.test((IMath) (x, y) -> x + y);
    
    

    2. 变量引用

    lambda 表达式是实现了一个接口的内部匿名类,它的引用变量和我们的匿名类的引用变量规则是一样的。

    我们先来看一个例子:

    /**
     * 变量引用
     */
    public class VarDemo {
        public static void main(String[] args) {
            String str = "hello world";
            Consumer<String> consumer = s -> System.out.println(s + str);
            consumer.accept("lambda ");
        }
    }
    

    运行 main 方法,打印输出


    image

    回想 JDK8 之前,内部类里面,我们引用外部变量时,这个变量必须声明为 final 类型(常量)。

    那么在 JDK8 里面呢?是不是不需要声明为常量了?不,其实还是要的,只不过在 JDK8 里面,你可以不写 final,它自动默认帮你加上 final。

    要证明很简单,在上面的代码

    String str = "hello world";
    

    后面再对 str 赋值,如:

    str = "ddd"; 
    

    你会发现,加上去会报错的,


    image

    报错提示你 str 是 final 的。所以写代码时,最好声明一下

    final String str = "hello world";
    

    为什么匿名类引用外面的变量必须是 final ?

    因为 java 里面,参数传参是传值的,而不是传引用!

    例如:

    List<String> list = new ArrayList<>();
    Consumer<String> consumer = s -> System.out.println(s + list);
    consumer.accept("lambda ");
    

    这个代码这样写是没问题的,为什么?这是因为在 System out 的时候,传进来的 list,实际上传的是 new ArrayList<>() 这一个对象。也就是说,在外面有一个 list 变量,在匿名类里面 也有一个 list 变量,这两个对象都指向 new ArrayList<>() 这一个对象,这就是传值 。

    如果你再对对外面的 list 进行赋值,修改引用,对匿名函数的 list 来说是没有影响的(还是指向 new ArrayList<>() 这一个对象),这时候运行代码,结果可能就不是你预想的。

    所以在 java 里面,内部类要使用外部变量时,外部变量一定是不能修改的!!这样才能保证 你里面的变量 和 外面的变量都是指向同一个变量。这样就不会造成 二义性了。

    代码地址: https://github.com/hmilyos/lambda-demo.git
    

    相关文章

      网友评论

        本文标题:5. 类型推断和变量引用

        本文链接:https://www.haomeiwen.com/subject/mxopjqtx.html