📄 x23614.html
字号:
<HTML><HEAD><TITLE>GridTurtle example</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.53"><LINKREL="HOME"TITLE="Documentation Set for Swarm 2.1.1"HREF="set.html"><LINKREL="UP"TITLE="Library Interface Conventions"HREF="swarm.library.sgml.appendix.html"><LINKREL="PREVIOUS"TITLE="Object Type Definitions"HREF="x23608.html"><LINKREL="NEXT"TITLE="Global Object Symbols"HREF="x23625.html"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Documentation Set for Swarm 2.1.1</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="x23608.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Appendix B. Library Interface Conventions</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="x23625.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="AEN23614">B.4. GridTurtle example</A></H1><P>An example will help illustrate the features of type definitionssupported by the defobj library. Throughout documentation of the basicSwarm libraries, a series of running examples will be based on asimple type of object belonging to sample Swarm simulations. This typeof object is an agent that move around on a two-dimensional grid,always moving in a current direction that it maintainsinternally. This agent is like a "turtle" of the original Logo system,except that its position is constrained to discrete integer values ofits X-Y coordinates, and its direction is always one of the fourorthogonal directions north, east, south, or west.</P><P>Following is a complete library header file for a library which defines such an object, called GridTurtle:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> /*Name: GridTurtle.hDescription: object type for Swarm example programsLibrary: grid*/#import <defobj.h>@deftype GridTurtle <Create, Drop, CREATABLE>CREATING- (void) setXLocMax: (int)xLocMax;- (void) setYLocMax: (int)yLocMax;SETTING- (void) setXLoc: (int)xLoc;- (void) setYLoc: (int)yLoc;- (void) setXLoc: xLoc setYLoc: yLoc;- (void) setDirection: direction;USING- (int) getXLoc;- (int) getYLoc;- getDirection;- (void) move: (int)distance;- (void) turn: (int)angle; // angle measured in units of pi/2 (90 deg.)- (void) print;@endid <Symbol> North, East, South, West;#import "grid.xt" </PRE></TD></TR></TABLE><P>An object type is defined by an @deftype declaration. (Note:newer libraries, including objectbase and random, now follow thelibrary interface conventions without using this special @deftypetag. Instead they use just an ordinary @protocol declaration, butotherwise they follow all the structure explained in this document.)The syntax of such declaration is identical to that of an Objective C@protocol definition, except for the entirely uppercase keywords(CREATABLE, CREATING, SETTING, USING) appearing in the GridTurtleexample above. All these modifications of Objective C syntax areaccomplished by simple preprocessor macros; no extensions to thelanguage compiler are involved.</P><P>When this library header file is processed (by a special rule ina make file), an external object id with the name GridTurtle isautomatically published. The name of a defined type becomes anordinary object that accepts specific messages defined by the defobjlibrary. The <AHREF="swarm.defobj.sgml.reference.html">defobjlibrary</A> explains the details of such messages; the only purposehere is to explain the basic sections of a deftype declaration.</P><P>deftype declarations follow the syntax as Objective C protocolsfor inheriting messages from each other: a list of names enclosed inangle brackets (e.g., <Create, Drop, ...> above) gives the namesof other declared types containing messages to be supported by the newtype as well. (These types referenced here are defined by the importedfile <defobj.h>.) Like protocols, full multiple inheritance oftypes is supported. The same messages may be inherited any number oftimes through any path with no different effect than if inherited ordeclared just once, so long as no conflicts occur in any of theirargument or return types.</P><P>The CREATABLE tag appearing in the inherited type list above isa special type which defines no messages of its own, but merely marksthe type as one which supports direct creation of instances of thetype. Without this tag, the only role of a type is to define messagesfor inheritance by other types. With this tag, the global type objecthas a particular implementation that supports object creation usingstandard messages defined in defobj.</P><P>The declared messages of the type may be separated into sectionsmarked by the special uppercase tags such as CREATING, SETTING, andUSING above. (Currently, these are the only such tags which mayoccur.) These sections each define messages belonging to a particulardefined "interface" of the object type, which are further combinedinto distinct "phases" of an object lifecycle supported by defobjmessages. Further explanation of the interfaces and phases defined bythis example are provided in the Usage Guide of the defobjlibrary.</P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="x23608.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="set.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="x23625.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Object Type Definitions</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="swarm.library.sgml.appendix.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Global Object Symbols</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -