作为练习,我应该实现一个特征 PartialOrdered[T]。
trait PartialOrdered[T] {
def below(that: T): Boolean
def < (that: T): Boolean = (this below that) && !(that below this)
/* followed by other relations <=, >, >=, ==, .. */
}
扩展此特征的类 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)
^
那么我错过了什么?提前致谢
编辑:这是一个示例类 Rectangle(在坐标系中),给出了两个相对的角,如果完全包含一个矩形,则它在另一个下方
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) = {...}
}