Object REXX

Contributed by Chris Rathman

OREXX Script (polymorph.cmd)

TryMe()
exit 0

TryMe:
   procedure

   /* set up some shape instances */
   scribble = .Array~new(2)
   scribble[1] = .Rectangle~new(10, 20, 5, 6)
   scribble[2] = .Circle~new(15, 25, 8)

   /* iterate through the array and handle shapes polymorphically */
   do i = 1 to 2
      scribble[i]~draw()
      scribble[i]~rMoveTo(100, 100)
      scribble[i]~draw()
   end

   /* access a rectangle specific function */
   rect = .Rectangle~new(0, 0, 15, 15)
   rect~setWidth(30)
   rect~draw()


::CLASS Shape

   /* shape constructor */
   ::METHOD init
      parse arg newx, newy
      Self~moveTo(newx, newy)

   /* x & y accessors */
   ::METHOD getX
      expose x
      return x

   ::METHOD getY
      expose y
      return y

   ::METHOD setX
      expose x
      parse arg newx
      x = newx

   ::METHOD setY
      expose y
      parse arg newy
      y = newy

   /* move the x & y position of the object */
   ::METHOD moveTo
      parse arg newx, newy
      Self~setX(newx)
      Self~setY(newy)

   ::METHOD rMoveTo
      parse arg newx, newy
      Self~moveTo(newx + Self~getX(), newy + Self~getY())

   /* pseudo abstract method - not necessary */
   ::METHOD draw


::CLASS Rectangle SUBCLASS "Shape"

   /* rectangle constructor */
   ::METHOD init
      parse arg newx, newy, newwidth, newheight
      Self~setWidth(newwidth)
      Self~setHeight(newheight)
      FORWARD CLASS(Super) ARRAY(newx, newy)

   /* width & height accessors */
   ::METHOD getWidth
      expose width
      return width

   ::METHOD getHeight
      expose height
      return height

   ::METHOD setWidth
      expose width
      parse arg newwidth
      width = newwidth

   ::METHOD setHeight
      expose height
      parse arg newheight
      height = newheight

   /* draw the rectangle */
   ::METHOD draw
      Say "Drawing a Rectangle at:("Self~getX()","Self~getY()"), Width" ,
         Self~getWidth()", Height" Self~getHeight()


::CLASS Circle SUBCLASS "Shape"

   /* circle constructor */
   ::METHOD init
      parse arg newx, newy, newradius
      Self~setRadius(newradius)
      FORWARD CLASS(Super) ARRAY(newx, newy)

   /* radius accessors */
   ::METHOD getRadius
      expose radius
      return radius

   ::METHOD setRadius
      expose radius
      parse arg newradius
      radius = newradius

   /* draw the circle */
   ::METHOD draw
      Say "Drawing a Circle at:("Self~getX()","Self~getY()"), Radius" Self~getRadius()

Output

Drawing a Rectangle at:(10,20), Width 5, Height 6
Drawing a Rectangle at:(110,120), Width 5, Height 6
Drawing a Circle at:(15,25), Radius 8
Drawing a Circle at:(115,125), Radius 8
Drawing a Rectangle at:(0,0), Width 30, Height 15

Chris Rathman / Chris.Rathman@tx.rr.com