2
trait Ordered[A] extends java.lang.Comparable[A] {
def compare(that: A): Int
def < (that: A): Boolean = (this compare that) < 0
def > (that: A): Boolean = (this compare that) > 0
def <= (that: A): Boolean = (this compare that) <= 0
def >= (that: A): Boolean = (this compare that) >= 0
def compareTo(that: A): Int = compare(that)
}
同時擁有compare
和compareTo
是不是有點無用? 我在這裏失去的巨大好處是什麼?Ordered [A]爲什麼使用比較方法而不是重用compareTo?
如果他們剛剛使用compareTo
我可以在我的代碼中將Comparable
替換爲Ordered
並完成。
請問訂購前期可比?這很奇怪,但至少有一個實現調用另一個。 – 2010-12-23 18:19:26
命名法。這只是Scala與大Java不同的方式。 – 2010-12-23 20:07:49