TclOO Package

Artifact [1a4e3bc3ac]
Login

Artifact 1a4e3bc3acdcaabdb58c70e0c3835dc9f1047c2d:

Wiki page [Doc: oo::class] by dkf 2016-09-17 10:52:18.
D 2016-09-17T10:52:18.205
L Doc:\soo::class
N text/x-markdown
P 8daa1ea485cd4cba4522b84e49937fb854cb1452
U dkf
W 6350
<h1>oo::class</h1>
<DL style="font-size: .85em">
<DD><A HREF="#M2" NAME="L182">NAME</A>
<DL><DD>oo::class &mdash; class of all classes</DD></DL>
<DD><A HREF="#M3" NAME="L183">SYNOPSIS</A>
<DL>
</DL>
<DD><A HREF="#M4" NAME="L184">CLASS HIERARCHY</A>
<DD><A HREF="#M5" NAME="L185">DESCRIPTION</A>
<DD><A HREF="#M6" NAME="L186">CONSTRUCTOR</A>
<DD><A HREF="#M7" NAME="L187">DESTRUCTOR</A>
<DD><A HREF="#M8" NAME="L188">EXPORTED METHODS</A>
<DL class="exported methods">
<DD><A HREF="#M9" NAME="L189"><I>cls </I><B>create </B><I>name </I>?<I>arg ...</I>?</A>
<DD><A HREF="#M10" NAME="L190"><I>cls </I><B>new </B>?<I>arg ...</I>?</A>
</DL>
<DD><A HREF="#M11" NAME="L191">NON-EXPORTED METHODS</A>
<DL class="non-exported methods">
<DD><A HREF="#M12" NAME="L192"><I>cls </I><B>createWithNamespace</B><I> name nsName</I> ?<I>arg ...</I>?</A>
</DL>
<DD><A HREF="#M13" NAME="L193">EXAMPLES</A>
<DD><A HREF="#M14" NAME="L194">SEE ALSO</A>
<DD><A HREF="#M15" NAME="L195">KEYWORDS</A>
</DL>
<H3><A NAME="M2">NAME</A></H3>
<b>oo::class</b> &mdash; class of all classes
<H3><A NAME="M3">SYNOPSIS</A></H3>
package require TclOO<BR>
<BR>
<B>oo::class</B><I> method </I>?<I>arg ...</I>?<BR>
<H3><A NAME="M4">CLASS HIERARCHY</A></H3>
<B><A HREF="?name=Doc:+oo::object">oo::object</A></B><BR>
   <font size="+1">&#8594;</font> <B>oo::class</B><BR>
<H3><A NAME="M5">DESCRIPTION</A></H3>
Classes are objects that can manufacture other objects according to a pattern
stored in the factory object (the class). An instance of the class is created
by calling one of the class's factory methods, typically either <B>create</B>
if an explicit name is being given, or <B>new</B> if an arbitrary unique name
is to be automatically selected.
<P>
The <B>oo::class</B> class is the class of all classes; every class is an
instance of this class, which is consequently an instance of itself. This
class is a subclass of <B><A HREF="?name=Doc:+oo::object">oo::object</A></B>, so every class is also an object.
Additional metaclasses (i.e., classes of classes) can be defined if necessary
by subclassing <B>oo::class</B>. Note that the <B>oo::class</B> object hides the
<B>new</B> method on itself, so new classes should always be made using the
<B>create</B> method.
<H4><A NAME="M6">CONSTRUCTOR</A></H4>
The constructor of the <B>oo::class</B> class takes an optional argument which,
if present, is sent to the <B><A HREF="?name=Doc:+oo::define">oo::define</A></B> command (along with the name of
the newly-created class) to allow the class to be conveniently configured at
creation time.
<H4><A NAME="M7">DESTRUCTOR</A></H4>
The <B>oo::class</B> class does not define an explicit destructor. However,
when a class is destroyed, all its subclasses and instances are also
destroyed, along with all objects that it has been mixed into.
<H4><A NAME="M8">EXPORTED METHODS</A></H4>
<DL class="exported methods">
<DT><A NAME="M9"><I>cls </I><B>create </B><I>name </I>?<I>arg ...</I>?</A><DD>
This creates a new instance of the class <I>cls</I> called <I>name</I> (which is
resolved within the calling context's namespace if not fully qualified),
passing the arguments, <I>arg ...</I>, to the constructor, and (if that returns
a successful result) returning the fully qualified name of the created object
(the result of the constructor is ignored). If the constructor fails (i.e.
returns a non-OK result) then the object is destroyed and the error message is
the result of this method call.
<P><DT><A NAME="M10"><I>cls </I><B>new </B>?<I>arg ...</I>?</A><DD>
This creates a new instance of the class <I>cls</I> with a new unique name,
passing the arguments, <I>arg ...</I>, to the constructor, and (if that returns
a successful result) returning the fully qualified name of the created object
(the result of the constructor is ignored). If the constructor fails (i.e.,
returns a non-OK result) then the object is destroyed and the error message is
the result of this method call.
<P>
Note that this method is not exported by the <B>oo::class</B> object itself, so
classes should not be created using this method.
<P></DL>
<H4><A NAME="M11">NON-EXPORTED METHODS</A></H4>
The <B>oo::class</B> class supports the following non-exported methods:
<P>
<DL class="non-exported methods">
<DT><A NAME="M12"><I>cls </I><B>createWithNamespace</B><I> name nsName</I> ?<I>arg ...</I>?</A><DD>
This creates a new instance of the class <I>cls</I> called <I>name</I> (which is
resolved within the calling context's namespace if not fully qualified),
passing the arguments, <I>arg ...</I>, to the constructor, and (if that returns
a successful result) returning the fully qualified name of the created object
(the result of the constructor is ignored). The name of the instance's
internal namespace will be <I>nsName</I> unless that namespace already exists
(when an arbitrary name will be chosen instead). If the constructor fails
(i.e., returns a non-OK result) then the object is destroyed and the error
message is the result of this method call.
<P></DL>
<H3><A NAME="M13">EXAMPLES</A></H3>
This example defines a simple class hierarchy and creates a new instance of
it. It then invokes a method of the object before destroying the hierarchy and
showing that the destruction is transitive.
<P>
<PRE><B>oo::class create</B> fruit {
    method eat {} {
        puts &quot;yummy!&quot;
    }
}
<B>oo::class create</B> banana {
    superclass fruit
    constructor {} {
        my variable peeled
        set peeled 0
    }
    method peel {} {
        my variable peeled
        set peeled 1
        puts &quot;skin now off&quot;
    }
    method edible? {} {
        my variable peeled
        return $peeled
    }
    method eat {} {
        if {![my edible?]} {
            my peel
        }
        next
    }
}
set b [banana <B>new</B>]
$b eat               <I><font size="+1">&#8594;</font> prints &quot;skin now off&quot; and &quot;yummy!&quot;</I>
fruit destroy
$b eat               <I><font size="+1">&#8594;</font> error &quot;unknown command&quot;</I></PRE>
<H3><A NAME="M14">SEE ALSO</A></H3>
<B><A HREF="?name=Doc:+oo::define">oo::define</A></B>, <B><A HREF="?name=Doc:+oo::object">oo::object</A></B>
<H3><A NAME="M15">KEYWORDS</A></H3>
class, metaclass, object

Z 5aad618edb8c6f344aaa638f9982f61f