Contributed by Chris Rathman
coclass'pShape' NB. attributes used by the class xpos =: 0 ypos =: 0 NB. class constructor create =: monad : 0 moveTo (0 } y.), (1 } y.) ) NB. class destructor destroy =: codestroy NB. accessors for x & y coordinates getX =: monad : 'xpos' getY =: monad : 'ypos' setX =: monad : 'xpos =: y.' setY =: monad : 'ypos =: y.' NB. move the x & y coordinates moveTo =: monad : 0 setX 0 } y. setY 1 } y. '' ) rMoveTo =: monad : 0 moveTo ((getX '') + (0 } y.)), ((getY '') + (1 } y.)) ) NB. pseudo virtual routine - draw the shape draw =: monad : '' |
coclass'pRectangle' coextend'pShape' NB. attributes used by the class width =: 0 height =: 0 NB. class constructor create =: monad : 0 NB. supposed to be able to call overridden superclass method, but this NB. appears to be broken as both elements in the scribble array end up NB. with the same coordinates. Just call moveTo directly instead. NB. create_pShape_ (0 } y.), (1 } y.) moveTo (0 } y.), (1 } y.) setWidth 2 } y. setHeight 3 } y. '' ) NB. clas destructor destroy =: codestroy NB. accessors for width & height getWidth =: monad : 'width' getHeight =: monad : 'height' setWidth =: monad : 'width =: y.' setHeight =: monad : 'height =: y.' NB. draw the rectangle draw =: monad : 0 WriteConsole__ 'Drawing a Rectangle at:(', (": getX''), ',', (": getY'') WriteConsole__ '), Width ', (": getWidth''), ' Height ', (": getHeight''), LF ) |
coclass'pCircle' coextend'pShape' NB. attributes used by the class radius =: 0 NB. class constructor create =: monad : 0 NB. supposed to be able to call overridden superclass method, but this NB. appears to be broken as the both elements in the scribble array NB. end up with the same coordinates. Just call moveTo directly instead. NB. create_pShape_ (0 } y.), (1 } y.) moveTo (0 } y.), (1 } y.) setRadius 2 } y. '' ) NB. class destructor destroy =: codestroy NB. accessors for the radius getRadius =: monad : 'radius' setRadius =: monad : 'radius =: y.' NB. draw the circle draw =: monad : 0 WriteConsole__ 'Drawing a Circle at:(', (": getX''), ',', (": getY'') WriteConsole__ '), Radius ', (": getRadius''), LF ) |
load 'user/classes/pShape.ijs' load 'user/classes/pCircle.ijs' load 'user/classes/pRectangle.ijs' NB. J does not seem to have a notion of stdout, so I am using the NB. Console package found on Oleg Kobchenko's J Page. The package NB. can be found at: http://members.xoom.com/olegyk/jpage.html load 'user/console' NB. define the test function TryMe =: monad : 0 NB. open up a console window for output AllocConsole'' NB. create some shape instances scribble =. (10 20 5 6 conew 'pRectangle'), (15 25 8 conew 'pCircle') NB. iterate through the list and handle shapes polymorphically i =. 0 while. i < # scribble do. temp =. i } scribble draw__temp'' rMoveTo__temp 100 100 draw__temp'' i =. i + 1 end. NB. call a rectangle specific function arect =. 0 0 15 15 conew 'pRectangle' setWidth__arect 30 draw__arect'' NB. deallocate the objects - no garbage collection i =. 0 while. i < # scribble do. temp =. i } scribble destroy__temp'' i =. i + 1 end. destroy__arect'' NB. close the console window FreeConsole'' ) NB. call the function to test polymorphism TryMe'' |
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 |