Java细节,Lambda表达式中使用this会怎么样?

Java细节,,Lambda表达式中使用this会怎么样?

最近根据需求,改了下安卓App的登录界面,里面计算短信发送时间的部分发现一个很有意思的地方。

一开始用的timer写的:

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @SuppressLint("SetTextI18n")
            public void run() {
                runOnUiThread(() -> {
                    if (timerCount > 0){
                        hintView.setText(timerCount + "秒");
                    }else {
                        hintView.setEnabled(true);
                        hintView.setText("重新发送");
                        timerCount = TIME_COUNT;
                        timer.cancel();
                    }
                    timerCount--;
                });
            }
        }, 0 , 1000);

后面想玩点骚操作,利用一下View的post函数计时:

        hintView.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (timerCount > 0){
                    hintView.setText(timerCount + "秒");
                    hintView.postDelayed(this, 1000);
                }else {
                    hintView.setEnabled(true);
                    hintView.setText("重新发送");
                    timerCount = TIME_COUNT;
                }
                timerCount--;
            }
        }, 1000);

那问题来了,我把这个Runnable写成Lambda表达式里面的this会怎么样呢?

先理解下 Lambda表达式。

Lambda表达式是对象吗?

也许根本不用问,大家都知道不是,根据《Java核心技术 卷一》所写:

lambda 表达式是一个可传递的代码块,可以在以后执行一次或多次。

Lambda表达式中使用this会怎么样?

sorry you,这里的this会报红,出错了,这里的this是外部类对象。

        hintView.postDelayed(() -> {
            if (timerCount > 0){
                hintView.setText(timerCount + "秒");
                hintView.postDelayed(this, 1000);
            }else {
                hintView.setEnabled(true);
                hintView.setText("重新发送");
                timerCount = TIME_COUNT;
            }
            timerCount--;
        }, 1000);

像这样
在这里插入图片描述
很神奇啊,明明在使用Runnable的时候完全没问题,改成Lambda表达式就不行了。

神奇的编译器

实际呢,还有一点很神奇,这里居然不能使用 Alt + enter
在这里插入图片描述
但是去掉带this的那一行,就可以了
在这里插入图片描述

结论

Lambda表达式中使用this实际使用的是当前类,而使用匿名类是可以获得this对象的。

lambda表达式,this关键字的作用与普通的Java的this关键字不同。在lambda表达式,this关键字指向创建lambda表达式的方法所在的类实例。 这与使用Runnable接口的匿名类不同,匿名类的this关键字指向匿名类实例本身。因此,在使用Runnable接口时,this关键字可以正常使用,但在lambda表达式使用this关键字可能出现问题。 这是由编译器的实现方式决定的。编译器lambda表达式生成一个类,并将其作为一个方法传递给函数式接口。因此,lambda表达式的this指向的是创建lambda表达式的方法所在的类实例,而不是lambda表达式本身。 所以,当你在lambda表达式使用this关键字时,请确保你理解它指向的是哪个对象。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Java细节Lambda表达式使用this怎么样?](https://blog.csdn.net/lfq88/article/details/113697991)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [java8(三)Lambda表达式的this](https://blog.csdn.net/u010825931/article/details/105774383)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值