为什么我不能将lambda赋给Object? [英] Why can't I assign lambda to Object?
本文介绍了为什么我不能将lambda赋给Object?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将lambda分配给Object类型:
I was trying to assign a lambda to Object type:
Object f = ()->{};
它给我错误说:
The target type of this expression must be a functional interface
为什么会发生这种情况,以及如何做到这一点?
Why is this happening, and how to do this?
推荐答案
这是不可能的。根据错误消息 Object
不是一个功能接口,这是一个具有单个公共方法的接口,因此您需要使用引用类型,例如
It's not possible. As per the error message Object
is not a functional interface, that is an interface with a single public method so you need to use a reference type that is, e.g.
Runnable r = () -> {};
这篇关于为什么我不能将lambda赋给Object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文