Overriding Comparison Operators in Groovy
I would like to override the> = operator in Groovy, found this page , but I'm still not sure how to do it, I have a Banknote class with serial and quantity properties, and I want to implement comparison bases on the amount property.
+3
Dan
source
to share
1 answer
You are not overriding the operator >=
, you are implementing compareTo
:
class Foo implements Comparable {
int val
int compareTo(Object o) { return val <=> ((Foo) o).val }
}
f1 = new Foo(val: 5)
f2 = new Foo(val: 10)
println f1 <= f2
=> true
+5
Dave newton
source
to share