作为练习,我应该实现一个特性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