📄 itcl_class.n
字号:
.SH CLASS USAGE.PPWhen a class definition has been loaded (or made available to theauto-loader), the class name can be used as a command..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. If construction issuccessful, the object name is returned and this name becomes a commandin the current namespace context. Otherwise, an error is returned..TP\fIclassName\fR #auto ?\fIargs...\fR?Creates a new object in class \fIclassName\fR with an automaticallygenerated name. Names are of the form \fIclassName<number>\fR,.VSwhere the \fIclassName\fR part is modified to start with a lowercaseletter. In class "Toaster", for example, the "\fB#auto\fR" specificationwould produce names toaster0, toaster1, etc. Remaining arguments are.VEpassed to the constructor. If construction is successful, the objectname is returned and this name becomes a command in the currentnamespace context. Otherwise, an error is returned..TP\fIclassName\fR :: \fIproc\fR ?\fIargs...\fR?Used outside of the class scope to invoke a class proc named \fIproc\fR.Class procs are like ordinary Tcl procs, except that they are executedin the scope of the class and therefore have transparentaccess to common data members..RS.LP.VSNotice that, unlike any other scope qualifier in \fB[incr\ Tcl]\fR, the "::"shown above is surrounded by spaces. This is unnecessary with thenew namespace facility, and is considered obsolete. The capabilityis still supported, however, to provide backward-compatibility withearlier versions..VE.RE.SH OBJECT USAGE.TP\fIobjName method\fR ?\fIargs...\fR?Invokes a method named \fImethod\fR to operate on the specified object.Remaining arguments are passed to the method. The method name canbe "constructor", "destructor", any method name appearing in theclass definition, or any of the following built-in methods..SH BUILT-IN METHODS.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 \fBdelete\fRInvokes the destructor associated with an object.If the destructor is successful, data associated with the object isdeleted and \fIobjName\fR is removed as a command from theinterpreter. Returns the empty string, regardless of the destructorbody..RS.LP.VSThe built-in \fBdelete\fR method has been replaced by the"\fBdelete object\fR" command in the global namespace, andis considered obsolete. The capability is still supported,however, to provide backward-compatibility with earlier versions..VE.RE.TP\fIobjName\fR \fBinfo \fIoption\fR ?\fIargs...\fR?Returns information related to the class definition or toa particular object named \fIobjName\fR.The \fIoption\fR parameter includes the following things, as well asthe options recognized by the usual Tcl "info" command:.RS.TP\fIobjName\fR \fBinfo class\fR.VSReturns the name of the most-specific class for object \fIobjName\fR..VE.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 classes inthe order that they are traversed for member lookup and objectdestruction..TP\fIobjName\fR \fBinfo method\fR ?\fImethodName\fR? ?\fB-args\fR? ?\fB-body\fR?With no arguments, this command returns a list of all class methods.If \fImethodName\fR is specified, it returns information for a specific method.If neither of the optional \fB-args\fR or \fB-body\fR flags is specified,a complete method definition is returned as a list of three elementsincluding the method name, argument list and body. Otherwise, therequested information is returned without the method name.If the \fImethodName\fR is not recognized, an empty string is returned..TP\fIobjName\fR \fBinfo proc\fR ?\fIprocName\fR? ?\fB-args\fR? ?\fB-body\fR?With no arguments, this command returns a list of all class procs.If \fIprocName\fR is specified, it returns information for a specific proc.If neither of the optional \fB-args\fR or \fB-body\fR flags is specified,a complete proc definition is returned as a list of three elementsincluding the proc name, argument list and body. Otherwise, therequested information is returned without the proc name.If the \fIprocName\fR is not recognized, an empty string is returned..TP\fIobjName\fR \fBinfo public\fR ?\fIvarName\fR? ?\fB-init\fR? ?\fB-value\fR? ?\fB-config\fR?With no arguments, this command returns a list of all public variables.If \fIvarName\fR is specified, it returns information for a specific publicvariable.If none of the optional \fB-init\fR, \fB-value\fR or \fB-config\fR flagsare specified, all available information is returned as a list of fourelements including the variable name, initial value, current value,and configuration commands. Otherwise, the requested information isreturned without the variable name.If the \fIvarName\fR is not recognized, an empty string is returned..TP\fIobjName\fR \fBinfo protected\fR ?\fIvarName\fR? ?\fB-init\fR? ?\fB-value\fR?With no arguments, this command returns a list of all protected variables.If \fIvarName\fR is specified, it returns information for a specific protectedvariable.If neither of the optional \fB-init\fR or \fB-value\fR flags is specified,all available information is returned as a list of three elementsincluding the variable name, initial value and current value.Otherwise, the requested information is returned without the variable name.If the \fIvarName\fR is not recognized, an empty string is returned..TP\fIobjName\fR \fBinfo common\fR ?\fIvarName\fR? ?\fB-init\fR? ?\fB-value\fR?With no arguments, this command returns a list of all common variables.If \fIvarName\fR is specified, it returns information for a specific commonvariable.If neither of the optional \fB-init\fR or \fB-value\fR flags is specified,all available information is returned as a list of three elementsincluding the variable name, initial value and current value.Otherwise, the requested information is returned without the variable name.If the \fIvarName\fR is not recognized, an empty string is returned..RE.SH OTHER BUILT-IN COMMANDSThe following commands are also available within the scope of each class.They cannot be accessed from outside of the class as proper methods orprocs; rather, they are useful inside the class when implementing itsfunctionality..TP\fBglobal \fIvarName\fR ?\fIvarName...\fR?Creates a link to one or more global variables in the currentnamespace context. Global variables can also be accessed inother namespaces by including namespace qualifiers in \fIvarName\fR.This is useful when communicating with Tk widgets that rely on globalvariables..TP\fBprevious \fIcommand\fR ?\fIargs...\fR?Invokes \fIcommand\fR in the scope of the most immediate base class.VS(i.e., the "previous" class) for the object. For classes using single.VEinheritance, this facility can be used to avoid hard-wired base classreferences of the form "\fIclass\fR::\fIcommand\fR", making code easierto maintain. For classes using multiple inheritance, the utility ofthis function is dubious.If the class at the relevant scope has no base class, an error is returned..TP\fBvirtual \fIcommand\fR ?\fIargs...\fR?.VSInvokes \fIcommand\fR in the scope of the most-specific class for theobject. The methods within a class are automatically virtual; wheneveran unqualified method name is used, it always refers to the most-specificimplementation for that method. This function provides a way ofevaluating code fragments in a base class that have access to themost-specific object information. It is useful, for example, forcreating base classes that can capture and save an object's state.It inverts the usual notions of object-oriented programming, however,and should therefore be used sparingly..VE.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 KEYWORDSclass, object, object-oriented
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -