📄 class.n
字号:
'\"'\" Copyright (c) 1993-1998 Lucent Technologies, Inc.'\"'\" See the file "license.terms" for information on usage and redistribution'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.'\"'\" RCS: $Id: class.n,v 1.1 2003/02/05 10:53:53 mdejong Exp $'\".so man.macros.TH class n "" itcl "[incr\ Tcl]".BS'\" Note: do not modify the .SH NAME line immediately below!.SH NAMEclass \- create a class of objects.SH SYNOPSIS\fBclass \fIclassName\fR \fB{.br \fBinherit \fIbaseClass\fR ?\fIbaseClass\fR...?.br \fBconstructor \fIargs\fR ?\fIinit\fR? \fIbody\fR.br \fBdestructor \fIbody\fR.br \fBmethod \fIname\fR ?\fIargs\fR? ?\fIbody\fR?.br \fBproc \fIname ?\fIargs\fR? ?\fIbody\fR?.br \fBvariable \fIvarName\fR ?\fIinit\fR? ?\fIconfig\fR?.br \fBcommon \fIvarName\fR ?\fIinit\fR?.sp \fBpublic \fIcommand\fR ?\fIarg arg ...\fR?.br \fBprotected \fIcommand\fR ?\fIarg arg ...\fR?.br \fBprivate \fIcommand\fR ?\fIarg arg ...\fR?.sp \fBset \fIvarName\fR ?\fIvalue\fR?.br \fBarray \fIoption\fR ?\fIarg arg ...\fR?.br\fB}\fR.sp\fIclassName objName\fR ?\fIarg arg ...\fR?.sp\fIobjName method\fR ?\fIarg arg ...\fR?.sp\fIclassName::proc ?\fIarg arg ...\fR?.BE.SH DESCRIPTION.PPThe fundamental construct in \fB[incr\ Tcl]\fR is the class definition.Each class acts as a template for actual objects that can be created.The class itself is a namespace which contains things common to allobjects. Each object has its own unique bundle of data which containsinstances of the "variables" defined in the class definition. Eachobject also has a built-in variable named "this", which contains thename of the object. Classes can also have "common" data members thatare shared by all objects in a class..PPTwo types of functions can be included in the class definition."Methods" are functions which operate on a specific object, andtherefore have access to both "variables" and "common" data members."Procs" are ordinary procedures in the class namespace, and onlyhave access to "common" data members..PPIf the body of any method or proc starts with "\fB@\fR", it is treatedas the symbolic name for a C procedure. Otherwise, it is treated asa Tcl code script. See below for details on registering and usingC procedures..PPA class can only be defined once, although the bodies of classmethods and procs can be defined again and again for interactivedebugging. See the \fBbody\fR and \fBconfigbody\fR commands fordetails..PPEach namespace can have its own collection of objects and classes.The list of classes available in the current context can be queriedusing the "\fBitcl::find classes\fR" command, and the list of objects,with the "\fBitcl::find objects\fR" command..PPA class can be deleted using the "\fBdelete class\fR" command.Individual objects can be deleted using the "\fBdelete object\fR"command..SH CLASS DEFINITIONS.TP\fBclass \fIclassName definition\fRProvides the definition for a class named \fIclassName\fR. Ifthe class \fIclassName\fR already exists, or if a command called\fIclassName\fR exists in the current namespace context, thiscommand returns an error. If the class definition is successfullyparsed, \fIclassName\fR becomes a command in the current context,handling the creation of objects for this class..PPThe class \fIdefinition\fR is evaluated as a series of Tclstatements that define elements within the class. The followingclass definition commands are recognized:.RS.TP\fBinherit \fIbaseClass\fR ?\fIbaseClass\fR...?Causes the current class to inherit characteristics from one ormore base classes. Classes must have been defined by a previous\fBclass\fR command, or must be available to the auto-loadingfacility (see "AUTO-LOADING" below). A single class definitioncan contain no more than one \fBinherit\fR command..spThe order of \fIbaseClass\fR names in the \fBinherit\fR listaffects the name resolution for class members. When the samemember name appears in two or more base classes, the base classthat appears first in the \fBinherit\fR list takes precedence.For example, if classes "Foo" and "Bar" both contain the member"x", and if another class has the "\fBinherit\fR" statement:.CSinherit Foo Bar.CEthen the name "x" means "Foo::x". Other inherited members named"x" must be referenced with their explicit name, like "Bar::x"..TP\fBconstructor \fIargs\fR ?\fIinit\fR? \fIbody\fRDeclares the \fIargs\fR argument list and \fIbody\fR used forthe constructor, which is automatically invoked whenever anobject is created..spBefore the \fIbody\fR is executed, theoptional \fIinit\fR statement is used to invoke any base classconstructors that require arguments. Variables in the \fIargs\fRspecification can be accessed in the \fIinit\fR code fragment,and passed to base class constructors. After evaluating the\fIinit\fR statement, any base class constructors that havenot been executed are invoked automatically without arguments.This ensures that all base classes are fully constructed beforethe constructor \fIbody\fR is executed. By default, thisscheme causes constructors to be invoked in order from least-to most-specific. This is exactly the opposite of the orderthat classes are reported by the \fBinfo heritage\fR command..spIf construction is successful, the constructor always returnsthe object name\-regardless of how the \fIbody\fR is defined\-andthe object name becomes a command in the current namespace context.If construction fails, an error message is returned..TP\fBdestructor \fIbody\fRDeclares the \fIbody\fR used for the destructor, which is automaticallyinvoked when an object is deleted. If the destructor is successful,the object data is destroyed and the object name is removed as a commandfrom the interpreter. If destruction fails, an error message is returnedand the object remains..spWhen an object is destroyed, all destructors in its class hierarchyare invoked in order from most- to least-specific. This is theorder that the classes are reported by the "\fBinfo heritage\fR"command, and it is exactly the opposite of the default constructororder..TP\fBmethod \fIname\fR ?\fIargs\fR? ?\fIbody\fR?Declares a method called \fIname\fR. When the method \fIbody\fR isexecuted, it will have automatic access to object-specific variablesand common data members..spIf the \fIargs\fR list is specified, it establishes the usageinformation for this method. The \fBbody\fR command can be usedto redefine the method body, but the \fIargs\fR list must matchthis specification..spWithin the body of another class method, a method can be invokedlike any other command\-simply by using its name. Outside of theclass context, the method name must be prefaced an object name,which provides the context for the data that it manipulates.Methods in a base class that are redefined in the current class,or hidden by another base class, can be qualified using the"\fIclassName\fR::\fImethod\fR" syntax..TP\fBproc \fIname\fR ?\fIargs\fR? ?\fIbody\fR?Declares a proc called \fIname\fR. A proc is an ordinary procedurewithin the class namespace. Unlike a method, a proc is invokedwithout referring to a specific object. When the proc \fIbody\fR isexecuted, it will have automatic access only to common data members..spIf the \fIargs\fR list is specified, it establishes the usageinformation for this proc. The \fBbody\fR command can be usedto redefine the proc body, but the \fIargs\fR list must matchthis specification..spWithin the body of another class method or proc, a proc can beinvoked like any other command\-simply by using its name.In any other namespace context, the proc is invoked using aqualified name like "\fIclassName\fB::\fIproc\fR". Procs ina base class that are redefined in the current class, or hiddenby another base class, can also be accessed via their qualifiedname..TP\fBvariable \fIvarName\fR ?\fIinit\fR? ?\fIconfig\fR?Defines an object-specific variable named \fIvarName\fR. Allobject-specific variables are automatically available in classmethods. They need not be declared with anything like the\fBglobal\fR command..spIf the optional \fIinit\fR string is specified, it is used as theinitial value of the variable when a new object is created.Initialization forces the variable to be a simple scalarvalue; uninitialized variables, on the other hand, can be setwithin the constructor and used as arrays..spThe optional \fIconfig\fR script is only allowed for public variables.If specified, this code fragment is executed whenever a publicvariable is modified by the built-in "configure" method. The\fIconfig\fR script can also be specified outside of the classdefinition using the \fBconfigbody\fR command..TP\fBcommon \fIvarName\fR ?\fIinit\fR?Declares a common variable named \fIvarName\fR. Common variablesreside in the class namespace and are shared by all objects belongingto the class. They are just like global variables, except thatthey need not be declared with the usual \fBglobal\fR command.They are automatically visible in all class methods and procs..spIf the optional \fIinit\fR string is specified, it is used as theinitial value of the variable. Initialization forces the variableto be a simple scalar value; uninitialized variables, on the otherhand, can be set with subsequent \fBset\fR and \fBarray\fR commandsand used as arrays..spOnce a common data member has been defined, it can be set using\fBset\fR and \fBarray\fR commands within the class definition.This allows common data members to be initialized as arrays.For example:.CSclass Foo { common boolean set boolean(true) 1 set boolean(false) 0}.CENote that if common data members are initialized within theconstructor, they get initialized again and again whenever newobjects are created..TP\fBpublic \fIcommand\fR ?\fIarg arg ...\fR?.TP\fBprotected \fIcommand\fR ?\fIarg arg ...\fR?.TP\fBprivate \fIcommand\fR ?\fIarg arg ...\fR?These commands are used to set the protection level for classmembers that are created when \fIcommand\fR is evaluated.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -