【发布时间】:2023-03-30 23:15:01
【问题描述】:
Groovy 具有宇宙飞船运算符<=>,它提供了一种实现比较的简单方法。我怎样才能以更时髦的方式链接它,然后是下面的代码?在这个例子中,我想先按价格比较商品,如果两者价格相同,再按名称比较。
class Item implements Comparable {
int price
String name
int compareTo(Item other) {
int result = price <=> other.price
if (result == 0) {
result = name <=> other.name
}
return result
}
}
【问题讨论】:
标签: groovy chaining spaceship-operator