''[[extracted from [https://groups.google.com/group/comp.lang.tcl/msg/b65107193572d549?hl=en%|%this c.l.t posting]]]'' For very basic usage, here's this. It assumes you know a bit about OO systems in general. **Basic TclOO Usage** Declare a class like this: ====== oo::class create MyExampleClass { constructor {args} {# like [proc] # Do something with the args... } # Public methods begin with lower-case letters method foo {bar boo} {# like [proc] puts "bar=$bar, boo=$boo" } destructor {# No arguments # Clean up here if necessary } } ====== Use that class/object like this: ====== MyExampleClass create exampleObj someArgumentToConstructor... exampleObj foo 1 2; # prints 'bar=1, boo=2' exampleObj destroy; # wipes the object out set obj [MyExampleClass new]; # 'Unnamed' object $obj foo 3 4; # prints 'bar=3, boo=4' $obj destroy ====== ***State*** Every object (i.e., every instance) has its own arbitrarily-named [namespace] that is not shared with anything else. It is the current namespace when a method (or constructor or destructor) is executing. Store state in there, perhaps like this: ====== oo::class create Example2 { constructor x { variable X $x } method addToX y { variable X set X [expr {$X + $y}] return } destructor { variable X puts "X is $X" } } Example2 create eg 3 eg addToX 5 eg addToX 2 eg destroy; # Prints 'X is 10' ====== ***Introspection*** Introspection is done by [info class], [info object] and (within methods only) [self]. For example, the name of an object's private namespace is given by `[info object] namespace $obj`. By itself, [self] returns the name of the currently-executing object. See the documentation for details. ***Private Methods*** Private methods are methods whose names don't begin with a lower-case letter. They can be invoked on an object by using [my] instead of the object name, like this: ====== ... method ThePrivateMethod ... ... method aPublicMethod ... { puts "before the private call" my ThePrivateMethod ... puts "after the private call" } ... ====== The [my] command is always located in the object's private namespace. This makes it ideal (with a very little trickery) for handling callbacks like traces and events. **Going Deeper** This is where we get into the next level of understanding about TclOO. ***Definitions*** Classes delegate their definitions to the command [oo::define], which you can also use directly to add or update definitions to a class. You can also use [oo::objdefine] to apply definitions directly to an object (almost as if it is an instance of its own private subclass; that's not what happens but it looks very similar). You can even update the definitions on a class after instances of it have been created and have those instances pick up the changes. After all, Tcl's a dynamic language! ====== oo::class create Example2a # One syntax style: single argument is definition script oo::define Example2a { constructor x { variable X $x } destructor { variable X puts "X is $X" } } Example2a create foo 3 # Other syntax style: multiple arguments give a single definition oo::define Example2a method addToX y { variable X set X [expr {$X + $y}] return } ====== **Expert stuff** Classes ''are'' objects themselves (instances of [oo::class]) which is why the syntax for making a class is so much like the syntax for making a named object. Because it's exactly the same. This is a deep rabbit-hole, but I'll stop writing this up here. :-) ---- [jblz] Other helpful resources include: The original http://www.tcl.tk/cgi-bin/tct/tip/257.html%|%TclOO TIP%|%, and its http://www.tcl.tk/cgi-bin/tct/tip/354.html%|%companion%|%. There is also quite a bit of general purpose TclOO code in the libraries of http://wiki.tcl.tk/15781%|%wub%|%, if one is looking for examples of TclOO in action. <>Object Orientation | Tutorial