obstcl defines two global commands: one to define a class, and one to delete an object. The command to define a class looks like this:
defclass classname classbody
The classname is the name of the new class being defined, while classbody is the class definition (with internal commands documented on the next page). Here is an example of a class definition:
defclass Foo { inherit Object option -one 1 ;# An option variable variable two 2 ;# An instance variable method three {four} { ;# A method set result [expr {$_options(-one) + $two + $four}] set two $four return $result } }In this case, we have defined a class named Foo, and given it an option variable named -one, an instance variable named two, and a method named three. When a class is defined, it creates an object creation procedure, which has this syntax:
classname objectname ?arg arg...?The first argument, objectname, is the name that will be used to refer to the object. Remaining arguments are passed to the onbject;s constructor method. (See next page.)
For example, I can create an instance of Foo like this:
set foo [Foo foo24 -one 7]
Having created this object, I can access it in various ways:
$foo cget -one
$foo configure -one 1
$foo three 4
obstcl allows a class to inherit from another class. In the above example, Foo inherited from the class Object, which is provided with obstcl. Here is another example:
defclass Bar { inherit Foo option -eggs green variable ham cooked method breakfast {} { return "$two $_options(-eggs) eggs and [three $this 4] $ham ham!" } }This class extends its superclass with additional variables and methods. Let's try it:
set bar [Bar mybar] puts [$bar breakfast]This will, of course, print the enthusiastic if slightly ungrammatical message, "2 green eggs and 7 cooked ham!" Note that the variable two is being referenced directly in the breakfast method, even though it is defined in the superclass Foo.
When you're done with your objects, you can delete them with this procedure:
deleteobj nameFor example:
deleteobj $foo deleteobj $bar