📄 class.n
字号:
The \fIcommand\fR is usually \fBmethod\fR, \fBproc\fR,\fBvariable\fR or\fBcommon\fR, and the remaining \fIarg\fR'scomplete the member definition. However, \fIcommand\fR canalso be a script containing many different member definitions,and the protection level will apply to all of the membersthat are created..SH CLASS USAGE.PPOnce a class has been defined, the class name can be used as acommand to create new objects belonging to the class..TP\fIclassName objName\fR ?\fIargs...\fR?Creates a new object in class \fIclassName\fR with the name \fIobjName\fR.Remaining arguments are passed to the constructor of the most-specificclass. This in turn passes arguments to base class constructors beforeinvoking its own body of commands. If construction is successful, acommand called \fIobjName\fR is created in the current namespace context,and \fIobjName\fR is returned as the result of this operation.If an error is encountered during construction, the destructors areautomatically invoked to free any resources that have been allocated,the object is deleted, and an error is returned..spIf \fIobjName\fR contains the string "\fB#auto\fR", that string isreplaced with an automatically generated name. Names have theform \fIclassName<number>\fR, where the \fIclassName\fR part ismodified to start with a lowercase letter. In class "Toaster",for example, the "\fB#auto\fR" specification would produce nameslike toaster0, toaster1, etc. Note that "\fB#auto\fR" can bealso be buried within an object name:.CSfileselectiondialog .foo.bar.#auto -background red.CEThis would generate an object named ".foo.bar.fileselectiondialog0"..SH OBJECT USAGEOnce an object has been created, the object name can be usedas a command to invoke methods that operate on the object..TP\fIobjName method\fR ?\fIargs...\fR?Invokes a method named \fImethod\fR on an object named \fIobjName\fR.Remaining arguments are passed to the argument list for themethod. The method name can be "constructor", "destructor",any method name appearing in the class definition, or any ofthe following built-in methods..SH BUILT-IN METHODS.TP\fIobjName\fR \fBcget option\fRProvides access to public variables as configuration options. Thismimics the behavior of the usual "cget" operation for Tk widgets.The \fIoption\fR argument is a string of the form "\fB-\fIvarName\fR",and this method returns the current value of the public variable\fIvarName\fR..TP\fIobjName\fR \fBconfigure\fR ?\fIoption\fR? ?\fIvalue option value ...\fR?Provides access to public variables as configuration options. Thismimics the behavior of the usual "configure" operation for Tk widgets.With no arguments, this method returns a list of lists describingall of the public variables. Each list has three elements: thevariable name, its initial value and its current value..spIf a single \fIoption\fR of the form "\fB-\fIvarName\fR" is specified,then this method returns the information for that one variable..spOtherwise, the arguments are treated as \fIoption\fR/\fIvalue\fRpairs assigning new values to public variables. Each variableis assigned its new value, and if it has any "config" code associatedwith it, it is executed in the context of the class where it wasdefined. If the "config" code generates an error, the variableis set back to its previous value, and the \fBconfigure\fR methodreturns an error..TP\fIobjName\fR \fBisa \fIclassName\fRReturns non-zero if the given \fIclassName\fR can be found in theobject's heritage, and zero otherwise..TP\fIobjName\fR \fBinfo \fIoption\fR ?\fIargs...\fR?Returns information related to a particular object named\fIobjName\fR, or to its class definition. The \fIoption\fRparameter includes the following things, as well as the optionsrecognized by the usual Tcl "info" command:.RS.TP\fIobjName\fR \fBinfo class\fRReturns the name of the most-specific class for object \fIobjName\fR..TP\fIobjName\fR \fBinfo inherit\fRReturns the list of base classes as they were defined in the"\fBinherit\fR" command, or an empty string if this classhas no base classes..TP\fIobjName\fR \fBinfo heritage\fRReturns the current class name and the entire list of base classesin the order that they are traversed for member lookup and objectdestruction..TP\fIobjName\fR \fBinfo function\fR ?\fIcmdName\fR? ?\fB-protection\fR? ?\fB-type\fR? ?\fB-name\fR? ?\fB-args\fR? ?\fB-body\fR?With no arguments, this command returns a list of all class methodsand procs. If \fIcmdName\fR is specified, it returns informationfor a specific method or proc. If no flags are specified, thiscommand returns a list with the following elements: the protectionlevel, the type (method/proc), the qualified name, the argument listand the body. Flags can be used to request specific elements fromthis list..TP\fIobjName\fR \fBinfo variable\fR ?\fIvarName\fR? ?\fB-protection\fR? ?\fB-type\fR? ?\fB-name\fR? ?\fB-init\fR? ?\fB-value\fR? ?\fB-config\fR?With no arguments, this command returns a list of all object-specificvariables and common data members. If \fIvarName\fR is specified, itreturns information for a specific data member. If no flags arespecified, this command returns a list with the following elements: theprotection level, the type (variable/common), the qualified name, theinitial value, and the current value. If \fIvarName\fR is a publicvariable, the "config" code is included on this list. Flags can beused to request specific elements from this list..SH CHAINING METHODS/PROCSSometimes a base class has a method or proc that is redefined withthe same name in a derived class. This is a way of making thederived class handle the same operations as the base class, butwith its own specialized behavior. For example, suppose we havea Toaster class that looks like this:.CSclass Toaster { variable crumbs 0 method toast {nslices} { if {$crumbs > 50} { error "== FIRE! FIRE! ==" } set crumbs [expr $crumbs+4*$nslices] } method clean {} { set crumbs 0 }}.CEWe might create another class like SmartToaster that redefinesthe "toast" method. If we want to access the base class method,we can qualify it with the base class name, to avoid ambiguity:.CSclass SmartToaster { inherit Toaster method toast {nslices} { if {$crumbs > 40} { clean } return [Toaster::toast $nslices] }}.CEInstead of hard-coding the base class name, we can use the"chain" command like this:.CSclass SmartToaster { inherit Toaster method toast {nslices} { if {$crumbs > 40} { clean } return [chain $nslices] }}.CEThe chain command searches through the class hierarchy fora slightly more generic (base class) implementation of a methodor proc, and invokes it with the specified arguments. It startsat the current class context and searches through base classesin the order that they are reported by the "info heritage" command.If another implementation is not found, this command does nothingand returns the null string..SH AUTO-LOADING.PPClass definitions need not be loaded explicitly; they can be loaded asneeded by the usual Tcl auto-loading facility. Each directory containingclass definition files should have an accompanying "tclIndex" file.Each line in this file identifies a Tcl procedure or \fB[incr\ Tcl]\fRclass definition and the file where the definition can be found..PPFor example, suppose a directory contains the definitions for classes"Toaster" and "SmartToaster". Then the "tclIndex" file for thisdirectory would look like:.CS# Tcl autoload index file, version 2.0 for [incr Tcl]# This file is generated by the "auto_mkindex" command# and sourced to set up indexing information for one or# more commands. Typically each line is a command that# sets an element in the auto_index array, where the# element name is the name of a command and the value is# a script that loads the command.set auto_index(::Toaster) "source $dir/Toaster.itcl"set auto_index(::SmartToaster) "source $dir/SmartToaster.itcl".PPThe \fBauto_mkindex\fR command is used to automaticallygenerate "tclIndex" files..CEThe auto-loader must be made aware of this directory by appendingthe directory name to the "auto_path" variable. When this is inplace, classes will be auto-loaded as needed when used in anapplication..SH C PROCEDURES.PPC procedures can be integrated into an \fB[incr\ Tcl]\fR classdefinition to implement methods, procs, and the "config" codefor public variables. Any body that starts with "\fB@\fR"is treated as the symbolic name for a C procedure..PPSymbolic names are established by registering procedures via\fBItcl_RegisterC()\fR. This is usually done in the \fBTcl_AppInit()\fRprocedure, which is automatically called when the interpreter starts up.In the following example, the procedure \fCMy_FooCmd()\fR is registeredwith the symbolic name "foo". This procedure can be referenced inthe \fBbody\fR command as "\fC@foo\fR"..CSintTcl_AppInit(interp) Tcl_Interp *interp; /* Interpreter for application. */{ if (Itcl_Init(interp) == TCL_ERROR) { return TCL_ERROR; } if (Itcl_RegisterC(interp, "foo", My_FooCmd) != TCL_OK) { return TCL_ERROR; }}.CEC procedures are implemented just like ordinary Tcl commands.See the \fBCrtCommand\fR man page for details. Within the procedure,class data members can be accessed like ordinary variablesusing \fBTcl_SetVar()\fR, \fBTcl_GetVar()\fR, \fBTcl_TraceVar()\fR,etc. Class methods and procs can be executed like ordinary commandsusing \fBTcl_Eval()\fR. \fB[incr\ Tcl]\fR makes this possible byautomatically setting up the context before executing the C procedure..PPThis scheme provides a natural migration path for code development.Classes can be developed quickly using Tcl code to implement thebodies. An entire application can be built and tested. Whennecessary, individual bodies can be implemented with C code toimprove performance..SH KEYWORDSclass, object, object-oriented
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -