MainComputersProgrammingLanguages › OO example

OO example

Edit Page
Report
Scan day: 16 February 2014 UTC
-49
Virus safety - good
Description: Erlang Code showing examples of Shapes using OO.
Rectangle class (rectangle.erl) -module(rectangle). -author('ChrisRathman'). -export([new/4,slots/4,dispatch/1]). % declare record to hold the slots for the class -record( rectangle, { x, y, width, height }). % populate the slots of the object record slots(X, Y, Width, Height) -> #rectangle { x = X, y = Y, width = Width, height = Height}. % create a process for the rectangle instance new(X, Y, Width, Height) -> This = slots(X, Y, Width, Height), spawn(rectangle, dispatch, [This]). % dispatch the messages for the process as they are received dispatch(This) -> receive {Pid, getx} -> Pid!{retval, getx(This)}, dispatch(This); {Pid, gety} -> Pid!{retval, gety(This)}, dispatch(This); {setx, X} -> dispatch(setx(This, X)); {sety, Y} -> dispatch(sety(This, Y)); {moveto, X, Y} -> dispatch(moveto(This, X, Y)); {rmoveto, X, Y} -> dispatch(rmoveto(This, X, Y)); {Pid, getwidth} -> Pid!{retval, getwidth(This)}, dispatch(This); {Pid, getheight} -> Pid!{retval, getheight(This)}, dispatch(This); {setwidth, Width} -> dispatch(setwidth(This, Width)); {setheight, Height} -> dispatch(setheight(This, Height)); {Pid, draw} -> draw(This), Pid!{retval, true}, dispatch(This); dispose -> true end. % get the x & y coordinates for the object getx(This) -> This#rectangle.x. gety(This) -> This#rectangle.y. % set the x & y coordinates for the object setx(This, X) -> This#rectangle{x = X}. sety(This, Y) -> This#rectangle{y = Y}. % move the x & y position of the object moveto(This, X, Y) -> setx(sety(This, Y), X). rmoveto(This, DeltaX, DeltaY) -> moveto(This, getx(This) + DeltaX, gety(This) + DeltaY). % get the width & height of the object getwidth(This) -> This#rectangle.width. getheight(This) -> This#rectangle.height. % set the width and height of the object setwidth(This, Width) -> This#rectangle{width = Width}. setheight(This, Height) -&
Size: 2048 chars

Contact Information

Phone&Fax:
Address:
Extended:

WEBSITE Info

Page title:Shapes OO example: Erlang Code
Keywords:
Description:
IP-address:209.202.252.41

WHOIS Info

NS
Name Server: NS1.LYCOS.COM
Name Server: NS2.LYCOS.COM
Name Server: NS3.LYCOS.COM
Name Server: NS4.LYCOS.COM
WHOIS
Status: clientTransferProhibited
Status: serverDeleteProhibited
Status: serverTransferProhibited
Status: serverUpdateProhibited
Date
Creation Date: 15-oct-1998
Expiration Date: 14-oct-2016