Skip to content

Instantly share code, notes, and snippets.

@bryanl
Created April 6, 2009 23:23
Show Gist options
  • Select an option

  • Save bryanl/91006 to your computer and use it in GitHub Desktop.

Select an option

Save bryanl/91006 to your computer and use it in GitHub Desktop.
trait Shape {
var x : Int
var y : Int
def draw
def moveTo(newx: Int, newy: Int) = {
x = newx
y = newy
}
def rMoveTo(dx: Int, dy: Int) = {
x += dx
y += dy
}
}
class Rectangle(var x: Int, var y: Int, var w: Int, var h: Int) extends Shape {
def draw = {
println("Drawing a Rectangle at (" + x + "," + y + "), width " + w + ", height " + h)
}
}
class Circle(var x: Int, var y: Int, var r: Int) extends Shape {
def draw = {
println("Drawing a Circle at (" + x + "," + y + "), radius " + r)
}
}
object TryShape {
def doSomethingWithShape(s: Shape) = {
s.draw
s.rMoveTo(100, 100)
s.draw
}
def main(args: Array[String]) = {
val shapes = new Array[Shape](2)
shapes(0) = new Rectangle(10, 20, 5, 6)
shapes(1) = new Circle(15, 25, 8)
for (shape <- shapes) {
TryShape.doSomethingWithShape(shape)
}
val rect = new Rectangle(0, 0, 15, 15)
rect.w = 30
rect.draw
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment