作爲練習,我應該實現一個特性PartialOrdered [T]。實現特質PartialOrdered [T]
trait PartialOrdered[T] {
def below(that: T): Boolean
def < (that: T): Boolean = (this below that) && !(that below this)
/* followed by other relations <=, >, >=, ==, .. */
}
擴展這一性狀A K類應該有以下實現,使得
a.below(b: K) = { true if a <= b,
false in any other case
然而,編譯提供了以下錯誤:
value below is not a member of type parameter T
def < (that: T): Boolean = (this below that) && !(that below this)
^
所以我缺少什麼?由於事先
編輯:這是一個示例類矩形(在座標系統中),具有兩個相對的角部給出的,其中,一個矩形低於另一如果它被完全包含
case class Rectangle (x1: Int, y1: Int, x2: Int, y2: Int)
extends PartialOrdered[Rectangle] {
def below(r: Rectangle): Boolean = {
val (rx, ry) = r.topLeft
val (tx, ty) = this.topLeft
tx >= rx && ty <= ry &&
tx + this.width <= rx + r.width &&
ty - this.height >= ry - r.height
}
def width: Int = {...}
def height: Int = {...}
def topLeft:(Int, Int) = {...}
}
這確實奏效,但超出了我之前學過的東西。我曾嘗試過類似的東西,它不適用於使用'<%'而不是'<:''this:T =>'的目的是什麼? – marius 2013-05-04 15:25:12