Re: Object Oriented Programming
- Posted by David Cuny <dcuny at LANSET.COM> Jun 30, 1999
- 617 views
Ralf wrote: >The real issue actually, is how variables are stored and looked up. David >uses a pretty readable approach in his Liama, although I couldn't completely >figure out how his class-handling works ... (david, where's the docs ?) You mean that code isn't all self-explanatory? [ general rules ] - Only one class should be declared per file. - The only global declared in a file should be the class id. - All identifiers should end in "_" to ensure they are private. [ declaring classes ] Classes are declared with the statement: global constant <classId> = derivedFrom( <superClassId>, <className> ) If you don't have a class to derive from, use a superclass of 0. For example: global constant BaseClass = deriveFrom( 0, "BaseClass" ) global constant HScrollBar = deriveFrom( ScrollBar, HScrollBar ) A derived class will inherit all the methods and local variables from it's superclass. [ declaring local variables ] Local variables are declared as: constant <local> = local( <classId>, <localName> ) For example: constant handle_ = local( BaseClass, "handle" ) If the local is already part of the class (inherited from a superclass), local() will not allocate extra storage for it. [ using local variables ] Locals can be accessed and updated with: my( <localIndex> ) setMy( <localIndex>, <value> ) incrMy( <localIndex>, <value> ) or my( <localName> ) setMy( <localName>, <value> ) incrMy( <localName>, <value> ) For example: setMy( handle_, hwnd ) foo = my( color_ ) bar = my( "color" ) incrMy( "counter", 1 ) [ assigning class methods ] Class methods are assigned using the syntax: method( <classId>, <methodName>, <routineId> ) For example: method( BaseClass, "new", routine_id("BaseClass_") ) Note that the object is not part of the method parameters (see next section). [ calling methods ] Methods can be called with the syntax: procedures: proc( <object>, <method>, <argList> ) functions: result = func( <object>, <method>, <argList> ) For example: dc = func( self, "getDC", {} ) proc( MyWindow, "setSize", {100, 200 } ) There is a special variable called 'self' that contains the id of the current object. This is set automatically when proc() or func() is invoked. [ creating objects ] The 'new' method has the syntax: <id> = new( <class>, <argList> ) For example: MyWindow = new( Window, { 100, 100, "Window Title" } ) The 'new' method automatically allocates storage for the object. To initialize an object, you will often want to call the 'new' method of it's superclass as well. This is accomplished with the classProc, which behaves the same as proc, but allows you to call the procedure of a specific class (in this case, the superclass): procedure SomeClass_() -- call the superclass's initialization classProc( self, BaseClass, {} ) -- initialize ... etc ... end procedure method( SomeClass, "new", routine_id("SomeClass_") ) [ destroying objects ] The 'destroy' method automaticall triggers the 'destroy' method of all the derived classes. So given the class structure of: Generic --> Control --> PushButton if you called: destroy( MyPushButton ) the method Generic.destroy would run first, then Control.destroy, and then finally PushButton.destroy; once all the methods had run, the space would be deallocated. That's about all there is to it. I'll try to write it up some time, when it stabilizes. Hope this helps! -- David Cuny