@amoszhou
2014-05-15T06:09:41.000000Z
字数 821
阅读 1277
scala课后习题
val egg = new java.awt.geom.Ellipse2D.Double(5,10,20,30) with RectangleLike
egg.translate(10,-10)
egg.grow(10,20)
不知道这样写,能不能符合题意:
trait RectangleLike{
this:Ellipse2D.Double=>
def translate(x:Double,y:Double){
this.x = x
this.y = y
}
def grow(x:Double,y:Double){
this.x += x
this.y += y
}
}
进去看了一下源码,Ordered是个带泛型的trait。有一个抽象的compare方法,那就是重写即可以
class OrderedPoint extends Point with Ordered[Point]{
def compare(that: Point): Int = if (this.x <= that.x && this.y < that.y) -1
else if(this.x == that.x && this.y == that.y) 0
else 1
}
其它题目好像没有会好写的,基本上只要理会了trait的本质,剩下的题,基本上都是类似的。