📄 body.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: body.n,v 1.1 2003/02/05 10:53:53 mdejong Exp $'\".so man.macros.TH body n 3.0 itcl "[incr\ Tcl]".BS'\" Note: do not modify the .SH NAME line immediately below!.SH NAMEbody \- change the body for a class method/proc.SH SYNOPSIS\fBbody \fIclassName\fB::\fIfunction args body\fR.BE.SH DESCRIPTION.PPThe \fBbody\fR command is used outside of an \fB[incr\ Tcl]\fRclass definition to define or redefine the body of a classmethod or proc. This facility allows a class definitionto have separate "interface" and "implementation" parts.The "interface" part is a \fBclass\fR command with declarationsfor methods, procs, instance variables and common variables.The "implementation" part is a series of \fBbody\fR and\fBconfigbody\fR commands. If the "implementation" partis kept in a separate file, it can be sourced again andagain as bugs are fixed, to support interactive development.When using the "tcl" mode in the \fBemacs\fR editor, the"interface" and "implementation" parts can be kept in thesame file; as bugs are fixed, individual bodies can behighlighted and sent to the test application..PPThe name "\fIclassName\fB::\fIfunction\fR"identifies the method/proc being changed..PPIf an \fIargs\fR list was specified when the \fIfunction\fR wasdefined in the class definition, the \fIargs\fR list for the\fBbody\fR command must match in meaning. Variable namescan change, but the argument lists must have the same requiredarguments and the same default values for optional arguments.The special \fBargs\fR argument acts as a wildcard when includedin the \fIargs\fR list in the class definition; it will matchzero or more arguments of any type when the body is redefined..PPIf the \fIbody\fR string starts with "\fB@\fR", it is treatedas the symbolic name for a C procedure. The \fIargs\fR listhas little meaning for the C procedure, except to documentthe expected usage. (The C procedure is not guaranteed touse arguments in this manner.) If \fIbody\fR does not startwith "\fB@\fR", it is treated as a Tcl command script. Whenthe function is invoked, command line arguments are matchedagainst the \fIargs\fR list, and local variables are createdto represent each argument. This is the usual behavior fora Tcl-style proc..PPSymbolic names for C procedures are established by registeringprocedures via \fBItcl_RegisterC()\fR. This is usually donein the \fBTcl_AppInit()\fR procedure, which is automatically calledwhen the interpreter starts up. In the following example,the procedure \fCMy_FooCmd()\fR is registered with thesymbolic 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; }}.CE.SH EXAMPLEIn the following example, a "File" class is defined to representopen files. The method bodies are included below the classdefinition via the \fBbody\fR command. Note that the bodiesof the constructor/destructor must be included in the classdefinition, but they can be redefined via the \fBbody\fR commandas well..CSclass File { private variable fid "" constructor {name access} { set fid [open $name $access] } destructor { close $fid } method get {} method put {line} method eof {}}body File::get {} { return [gets $fid]}body File::put {line} { puts $fid $line}body File::eof {} { return [::eof $fid]}## See the File class in action:#File x /etc/passwd "r"while {![x eof]} { puts "=> [x get]"}delete object x.CE.SH KEYWORDSclass, object, procedure
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -