#!/usr/bin/env e
class Rectangle(x, y, width, height) :any {
def rectanglestuff {
# accessors for x & y coordinates
to getX() :any { x }
to getY() :any { y }
to setX(newx) { x := newx }
to setY(newy) { y := newy }
# move the x & y coordinates
to moveTo(newx, newy) {
self setX(newx)
self setY(newy)
}
to rMoveTo(deltax, deltay) {
self moveTo((self getX()) + deltax, (self getY()) + deltay)
}
# accessors for width & height
to getWidth() :any { width }
to getHeight() :any { height }
to setWidth(newwidth) { width := newwidth }
to setHeight(newheight) { height := newheight }
# draw the rectangle
to draw() {
print("Drawing a Rectangle at:(")
print(x)
print(",")
print(y)
print("), width ")
print(width)
print(", height ")
println(height);
}
}
}
class Circle(x, y, radius) :any {
def circlestuff {
# accessors for x & y coordinates
to getX() :any { x }
to getY() :any { y }
to setX(newx) { x := newx }
to setY(newy) { y := newy }
# move the x & y coordinates
to moveTo(newx, newy) {
self setX(newx)
self setY(newy)
}
to rMoveTo(deltax, deltay) {
self moveTo((self getX()) + deltax, (self getY()) + deltay)
}
# accessors for the radius
to getRadius() :any { radius }
to setRadius(newradius) { radius := newradius }
# draw the circle
to draw() {
print("Drawing a Circle at:(")
print(self getX)
print(",")
print(self getY)
print("), radius ")
println(self getRadius)
}
}
}
def polymorph() {
# create some shape instances
def scribble := [Rectangle new(10, 20, 5, 6), Circle new(15, 25, 8)]
# iterate through the list and handle shapes polymorphically
for ashape in scribble {
ashape draw()
ashape rMoveTo(100, 100)
ashape draw()
}
# call a rectangle specific function
def rect := Rectangle new(0, 0, 15, 15)
rect setWidth(30)
rect draw()
}
polymorph();
|