在这种情况下(一个方法接口)可以使用lambda吗? [英] Is it possible to use lambdas in this case (One method interface)?
本文介绍了在这种情况下(一个方法接口)可以使用lambda吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
DialogInterface.OnClickListener closeOnOkClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
finish();
break;
}
}
};
我正在尝试将其转换为lambda表达式,但我无法做到.
And I am trying to convert this to a lambda expression but I cannot do it.
有可能吗?
如何?
推荐答案
有可能.每个仅有一种非默认方法的interface
都是FunctionalInterface
.注释仅用于编译器,以确保interface
仅获得了一个非默认方法,否则,您将获得编译器错误.
It is possible. Every interface
which just got one non-default method is an FunctionalInterface
. The annotation is just for the compiler to make sure the interface
just got one non-default method, if not you get a compiler error.
尝试一下:
DialogInterface.OnClickListener closeOnOkClickListener = (dialog, which) -> {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
finish();
break;
}
};
对此进行检查FunctionalInterface
注释的更大解释.
Check this out for a larger explaination of the FunctionalInterface
annotation.
这篇关于在这种情况下(一个方法接口)可以使用lambda吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文