我从 Java 中的 lambda 表达式开始,有一些我认为很奇怪的东西,我确信我做错了什么或者它有解决方法。
要定义比较器,我可以这样做:
col.setComparator((CustomCell o1, CustomCell o2) ->
((Comparable) o1.getValue()).compareTo(o2.getValue())
);
然而,如果我只添加两个“{”,那就太好了。我收到编译错误:
col.setComparator((CustomCell o1, CustomCell o2) -> {
((Comparable) o1.getValue()).compareTo(o2.getValue());
});
该错误与“{”无关,而是与setComparator
:
The method setComparator(Comparator<CustomCell>) in the type
TableColumnBase<CustomParentCell,CustomCell> is not applicable for the arguments
((CustomCell o1, CustomCell o2) -> {})
我之前尝试过对 actionevents 使用多行语句,它确实有效:
setOnAction(event -> {
// do something
});
是因为它只有一个参数吗?
您正在实施的方法setOnAction
is
public void handleEvent(ActionEvent event) ;
它的返回类型为void
:即它不返回任何内容:
您正在实施的方法setComparator
is
public int compare(CustomCell cell1, CustomCell cell2) ;
它返回一个值。要使用较长的形式,您必须为返回值的方法提供显式的 return 语句:
col.setComparator((CustomCell o1, CustomCell o2) -> {
return ((Comparable) o1.getValue()).compareTo(o2.getValue());
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)