Debian Codegraph @

docsrc/xlisp/xlisp-doc/tutorials/xlisp-objects.htm

raw
1 <html><head><title>XLISP Objects Primer</title>
2
3 <style type="text/css">
4 .example {
5 color: #000000;
6 background-color: #F5F5F5;
7 padding: 8px;
8 border: #808080;
9 border-style: solid;
10 border-width: 1px;
11 width:auto;
12 }
13 .button {
14 color: #000000;
15 background-color: #F5F5F5;
16 padding-top: 1px;
17 padding-bottom: 1px;
18 padding-left: 4px;
19 padding-right: 8px;
20 border: #808080;
21 border-style: solid;
22 border-width: 1px;
23 white-space: pre;
24 }
25 .box {
26 color: #000000;
27 padding-top: 4px;
28 padding-bottom: 4px;
29 padding-left: 16px;
30 padding-right: 16px;
31 border: #808080;
32 border-style: solid;
33 border-width: 1px;
34 }
35 </style>
36
37 </head>
38
39 <body>
40
41 <a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
42 <a href="../manual/contents.htm">Contents</a> |
43 <a href="tutorials.htm">Tutorials</a> |
44 <a href="../examples/examples.htm">Examples</a> |
45 <a href="../reference/reference-index.htm">Reference</a>
46
47 <hr>
48
49 <h1>XLISP Objects Primer</h1>
50
51 <hr>
52
53 <ol>
54 <li><nobr><a href="#1">Programming Styles</a></nobr></li>
55 <li><nobr><a href="#2">Object Oriented Programming</a></nobr></li>
56 <li><nobr><a href="#3">Xlisp Object Terminology</a></nobr></li>
57 <li><nobr><a href="#4">Sending Messages</a></nobr></li>
58 <li><nobr><a href="#5">Classes</a></nobr></li>
59 <li><nobr><a href="#6">A Better Class Example</a></nobr></li>
60 <li><nobr><a href="#7">Instances</a></nobr></li>
61 <li><nobr><a href="#8">Methods</a></nobr></li>
62 <li><nobr><a href="#9">Sending Messages To A Superclass</a></nobr></li>
63 <li><nobr><a href="#10">Object And Class</a></nobr></li>
64 <li><nobr><a href="#11">Objects Example</a></nobr></li>
65 </ol>
66
67 <p>This tutorial is adapted from a '<nobr>XLISPOOP.DOC</nobr>' document with
68 the following copyright:</p>
69
70 <p><div class="box">
71
72 <p>XLisp 2.0 Objects Primer by Tim I Mikkelsen - February 3, 1990</p>
73
74 <blockquote>
75
76 <p> Copyright (c) 1990 by Tim I. Mikkelsen. All Rights Reserved. No part of this
77 document may be copied, reproduced or translated for commercial use without
78 prior written consent of the author. Permission is granted for non-commercial
79 use as long as this notice is left intact.
80
81 <p> One of the features in the design of XLISP is object-oriented programming.
82 This primer is intended to serve as a very brief introduction to the object
83 facilities of the XLISP 2.0 dialect of LISP. Note that the object features of
84 XLISP are not based on other existing object definitions in other LISP dialects.
85 If you find problems in the primer, I'd appreciate hearing. </p>
86
87 </blockquote>
88
89 <p>Tim Mikkelsen, ([email protected]), 4316 Picadilly Drive, Fort Collins,
90 Colorado 80526</p>
91
92 </div></p>
93
94 <a name="1"></a>
95
96 <hr>
97
98 <h2>1&nbsp; Programming Styles</h2>
99
100 <hr>
101
102 <p>There are many programming models, some of <nobr>them are:</nobr></p>
103
104 <ul>
105 <li>procedural</li>
106 <li>functional</li>
107 <li><nobr>rule-based</nobr></li>
108 <li>declarative</li>
109 <li><nobr>object-oriented</nobr></li>
110 </ul>
111
112 <p>A language can have aspects of one or many of these programming
113 models.</p>
114
115 <p><div class="box">
116
117 <dl>
118
119 <dt><p><b>Procedure-Oriented</b></p></dt>
120
121 <dd><p>The programming paradigm most people are familiar with is the
122 procedural style. The primitives in procedural programming are subroutines
123 and data structures. Through these primitives, programmers have some limited
124 abilities to share programs and program fragments. <nobr>C and</nobr> Pascal
125 are examples of procedural languages. Some procedural languages <nobr>[such
126 as</nobr> Modula <nobr>and ADA]</nobr> have extensions that provide for
127 better sharing <nobr>of code</nobr>.</p></dd>
128
129 <dt><p><b>Object-Oriented</b></p></dt>
130
131 <dd><p><nobr>Object-oriented</nobr> programming is based on the primitives
132 of objects, classes and messages. Objects are defined in terms of classes.
133 Actions occur by sending a message to an object. <nobr>An object's</nobr>
134 definition can be inherited from more general classes.
135 <nobr>Objective-C</nobr> and C++ both are <nobr>object-oriented</nobr>
136 dialects of the <nobr>C language</nobr>. <nobr>Many dialects</nobr> of Lisp
137 have some <nobr>object-oriented</nobr> extension [Flavors, <nobr>Common
138 LOOPS</nobr>, CLOS and others]. There currently is standards work proceeding
139 to add <nobr>object-oriented</nobr> programming to <nobr>Common
140 Lisp</nobr>.</p></dd>
141
142 </dl>
143
144 </div></p>
145
146 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
147
148 <a name="2"></a>
149
150 <hr>
151
152 <h2>2&nbsp; Object Oriented Programming</h2>
153
154 <hr>
155
156 <p>The <nobr>object-oriented</nobr> programming model is based around the
157 concepts of objects, classes and messages. <nobr>An object</nobr> is
158 essentially a black box that contains internal state information. <nobr>You
159 send</nobr> an object a message which causes the object to perform some
160 operation. Objects are defined and described through classes.</p>
161
162 <p>One aspect of an object is that you do not have to know what is inside or
163 how it works to be able to <nobr>use it</nobr>. From a programming point of
164 view, this is very handy. <nobr>You can</nobr> develop a series of objects
165 for someone to use. <nobr>If you</nobr> need to change what goes on inside,
166 the users of the objects should be unaware.</p>
167
168 <p>Another aspect of objects is that of inheritance. <nobr>You can</nobr>
169 build up new classes from existing classes by inheriting the existing
170 class's functionality and then extending the new definition. For example,
171 you can define a 'tool' class with various attributes and then go about
172 creating object instances like '<nobr>tool-1</nobr>', '<nobr>tool-2</nobr>',
173 and <nobr>so on</nobr>. <nobr>You can</nobr> also create new
174 <nobr>sub-classes</nobr> of the 'tool' class like '<nobr>power-tool</nobr>'.
175 <nobr>This is</nobr> also very handy because you don't have to
176 <nobr>re-implement</nobr> something if you can build it up from
177 <nobr>existing code</nobr>.</p>
178
179 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
180
181 <a name="3"></a>
182
183 <hr>
184
185 <h2>3&nbsp; Xlisp Object Terminology</h2>
186
187 <hr>
188
189 <p>There are many different languages with <nobr>object-oriented</nobr>
190 extensions and facilities. <nobr>The terminology</nobr>, operations and
191 styles of these are very different. Some of the main definitions for XLISP's
192 <nobr>object-oriented</nobr> extensions are:</p>
193
194 <p><div class="box">
195
196 <dl>
197
198 <dt><p><nobr><b>object data type</b></nobr></p></dt>
199
200 <dd><p>The <a href="../reference/object.htm">object</a> data type is a
201 <nobr>built-in</nobr> data type of XLISP. Members of the object data type
202 are object instances and classes.</p></dd>
203
204 <dt><p><nobr><b>object instances</b></nobr></p></dt>
205
206 <dd><p>An 'object instance' is a composite structure that contains internal
207 state information, methods [the code which respond to messages], <nobr>a
208 pointer</nobr> to the object instance's defining class and a pointer to the
209 object's <nobr>super-class</nobr>. XLISP contains no <nobr>built-in</nobr>
210 object instances.</p></dd>
211
212 <dt><p><nobr><b>class objects</b></nobr></p></dt>
213
214 <dd><p>A <a href="../reference/class.htm">class</a> object is, essentially,
215 the template for defining the derived object instances. <nobr>A class
216 object</nobr>, although used differently from a simple object instance, is
217 structurally a member of the object data type. <nobr>It also</nobr> contains
218 the linking mechanism that allows you to build class hierarchies
219 <nobr>[sub-classes</nobr> and <nobr>super-classes]</nobr>. XLISP contains
220 two <nobr>built-in</nobr> class objects, 'object' and 'class'.</p></dd>
221
222 <dt><p><nobr><b>message selector</b></nobr></p></dt>
223
224 <dd><p>The 'message selector' is the symbol that is used to select a
225 particular action [method] from the object.</p></dd>
226
227 <dt><p><b>message</b></p></dt>
228
229 <dd><p>The 'message' is the combination of the message selector and the data
230 <nobr>[if any]</nobr> to be sent to the object.</p></dd>
231
232 <dt><p><b>method</b></p></dt>
233
234 <dd><p>The 'method' is the actual code that gets executed when the object
235 receives the message.</p></dd>
236
237 </dl>
238
239 </div></p>
240
241 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
242
243 <a name="4"></a>
244
245 <hr>
246
247 <h2>4&nbsp; Sending Messages</h2>
248
249 <hr>
250
251 <p>The mechanism for sending messages to XLISP objects is via the
252 <a href="../reference/send.htm">send</a> function. <nobr>It takes</nobr>
253 an object, a message selector and various optional arguments [depending on
254 the message selector].</p>
255
256 <p>The way that a user creates a new object is to
257 <a href="../reference/send.htm">send</a> a
258 <a href="../reference/keyword-new.htm">:new</a> message to a previously
259 defined class. <nobr>The result</nobr> of this
260 <a href="../reference/send.htm">send</a> will return an object, so this is
261 normally preceded by <nobr>a
262 <a href="../reference/setq.htm">setq</a></nobr>.</p>
263
264 <pre class="example">
265 &gt; (setq my-object (send object :new))
266 #&lt;Object: #2e100&gt;
267 </pre>
268
269 <p><div class="box">
270
271 <p><nobr>The examples</nobr> are similar to what you should see on your
272 computer screen. <nobr>The '&gt;'</nobr> is the normal XLISP prompt, the
273 characters that follow the prompt is what you type in to try the examples.
274 Note that XLISP prints objects together with their internal pointer, like
275 #2e100 in the example above. These <nobr>#ID numbers</nobr> may not match
276 with the numbers you see on the screen if you try the examples, but this is
277 not an error.</nobr></p>
278
279 </div></p>
280
281 <p> The object created above is of limited value. Most often, you create a
282 'class' object and then you create instances of that class. <nobr>So
283 in</nobr> the following example, a class called '<nobr>my-class</nobr>' is
284 created that inherits its definition from the a <nobr>built-in</nobr> <a
285 href="../reference/class.htm">class</a> definition. Then two instances are
286 created of the new class:</p>
287
288 <pre class="example">
289 &gt; (setq my-class (send class :new '()))
290 #&lt;Object: #27756&gt;
291
292 &gt; (setq my-instance (send my-class :new))
293 #&lt;Object: #27652&gt;
294
295 &gt; (setq another-instance (send my-class :new))
296 #&lt;Object: #275da&gt;
297 </pre>
298
299 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
300
301 <a name="5"></a>
302
303 <hr>
304
305 <h2>5&nbsp; Classes</h2>
306
307 <hr>
308
309 <p> In the examples above, a <a href="../reference/keyword-new.htm">:new</a>
310 message was used to create an object. <nobr>The message</nobr> used to see
311 what is in an object is the
312 <a href="../reference/keyword-show.htm">:show</a> message:</p>
313
314 <pre class="example">
315 &gt; (send my-class :show)
316 Object is #&lt;Object: #27756&gt;, Class is #&lt;Object: #23fe2&gt;
317 MESSAGES = NIL
318 IVARS = NIL
319 CVARS = NIL
320 CVALS = NIL
321 SUPERCLASS = #&lt;Object: #23fd8&gt;
322 IVARCNT = 0
323 IVARTOTAL = 0
324 #&lt;Object: #27756&gt;
325 </pre>
326
327 <p> From the display of the 'my-class' object you can see there are a
328 variety of components. The components of a class are:</p>
329
330 <p><div class="box">
331
332 <dl>
333
334 <dt><p><nobr><b>class pointer</b></nobr></p></dt>
335
336 <dd><p>This pointer shows to what class the object [instance or class]
337 belongs. For a class, this always points to the <nobr>built-in</nobr> object
338 <a href="../reference/class.htm">class</a>. This is also true of the
339 <a href="../reference/class.htm">class</a> object, its class pointer points
340 to itself.</p></dd>
341
342 <dt><p><nobr><b>superclass pointer</b></nobr></p></dt>
343
344 <dd><p>This pointer shows what the next class up the class <nobr>hierarchy
345 is</nobr>. <nobr>If the</nobr> user does not specify what class is the
346 superclass, it will point to the built-in <nobr>class
347 <a href="../reference/object.htm">object</a></nobr>.</p></dd>
348
349 <dt><p><b>messages</b></p></dt>
350
351 <dd><p>This component shows what messages are allowed for the class, and the
352 description of the method that will be used. <nobr>If the</nobr> method is
353 <nobr>system-defined</nobr>, it will show up in the <nobr>form
354 of:</nobr></p>
355
356 <pre class="example">
357 #&lt;Subr-: #18b98&gt;
358 </pre>
359
360 <p>Remember that the class hierarchy [through the superclass pointer] is
361 searched if the requested message is not found in the class.</p></dd>
362
363 <dt><p><nobr><b>instance variables</b></nobr></p></dt>
364
365 <dd><p>The IVARS component lists what instance variables will be created
366 when an object instance is created. <nobr>If no</nobr> instances of the
367 class exist, there are no instance variables. <nobr>If there</nobr> are
368 <nobr>5 instances</nobr> of a class, there are <nobr>5 complete</nobr> and
369 different groups of the instance variables.</p></dd>
370
371 <dt><p><nobr><b>class variables and values</b></nobr></p></dt>
372
373 <dd><p>The CVARS component lists what class variables exist within the
374 class. The CVALS component shows what the current values of the variables
375 are. Class variables are used to hold state information about a class. There
376 will be one of each of the class variables, independent of the number of
377 instances of the class created.</p></dd>
378
379 </dl>
380
381 </div></p>
382
383 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
384
385 <a name="6"></a>
386
387 <hr>
388
389 <h2>6&nbsp; A Better Class Example</h2>
390
391 <hr>
392
393 <p> The example shown in the previous section does work, but the class and
394 instances created don't really do anything of interest. The following
395 example sets up a tool class and creates some tool instances:</p>
396
397 <pre class="example">
398 &gt; (setq my-tools (send class :new '(power moveable operation)))
399 #&lt;Object: #277a6&gt;
400
401 &gt; (send my-tools :answer :isnew '(pow mov op)
402 '((setq power pow moveable mov operation op))
403 #&lt;Object: #277a6&gt;
404
405 &gt; (setq drill (send my-tools :new 'AC t 'holes))
406 #&lt;Object: #2ddbc&gt;
407
408 &gt; (setq hand-saw (send my-tools :new 'none t 'cuts))
409 #&lt;Object: #2dc40&gt;
410
411 &gt; (setq table-saw (send my-tools :new 'AC nil 'cuts))
412 #&lt;Object: #2db00&gt;
413 </pre>
414
415 <p>A class of objects called '<nobr>my-tools</nobr>' and three instances
416 were created:</p>
417
418 <p><nobr>&nbsp; <img alt="[Figure 1]" src="xobj-1.png"></nobr></p>
419
420 <p>First the class '<nobr>my-tools</nobr>' was created by sending the
421 <a href="../reference/keyword-new.htm">:new</a> message to the
422 <nobr>built-in</nobr> <a href="../reference/class.htm">class</a> object.
423 Then, within the '<nobr>my-tool</nobr>' class, three instances called
424 'drill', '<nobr>hand-saw</nobr>' and '<nobr>table-saw</nobr>' were
425 created by sending the <a href="../reference/keyword-new.htm">:new</a>
426 message to the '<nobr>my-tools</nobr>' class. Notice the three parameters
427 following the message selector. The instance variables are initialized
428 from these parameters by the :isnew method, inherited from the
429 '<nobr>my-tools</nobr>' class at the time when the instances were created.</p>
430
431 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
432
433 <a name="7"></a>
434
435 <hr>
436
437 <h2>7&nbsp; Instances</h2>
438
439 <hr>
440
441 <p>The following is a display of the contents of some of the instances
442 created above, where the XLISP object #ID numbers had been replaced by the
443 respective class and instance names:</p>
444
445 <pre class="example">
446 &gt; (send drill :show)
447 Object is #&lt;Object: #[<font color="#0066CC">drill</font>]&gt;, Class is #&lt;Object: #[<font color="#0066CC">my-tools</font>]&gt;
448 POWER = AC
449 MOVEABLE = T
450 OPERATION = HOLES
451 #&lt;Object: #[<font color="#0066CC">drill</font>]&gt;
452
453 &gt; (send hand-saw :show)
454 Object is #&lt;Object: #[<font color="#0066CC">hand-saw</font>]&gt;, Class is #&lt;Object: #[<font color="#0066CC">my-tools</font>]&gt;
455 POWER = NONE
456 MOVEABLE = T
457 OPERATION = CUTS
458 #&lt;Object: #[<font color="#0066CC">hand-saw</font>]&gt;
459 </pre>
460
461 <p>From the display of these instances you can see there are some
462 components and values. The components of an instance are:</p>
463
464 <p><div class="box">
465
466 <dl>
467
468 <dt><p><nobr><b>class pointer</b></nobr></p></dt>
469
470 <dd><p>This pointer shows to which class the current object instance
471 belongs. It is through this link that the system finds the methods to
472 execute for the received messages.</p></dd>
473
474 <dt><p><nobr><b>instance variables and values</b></nobr></p></dt>
475
476 <dd><p>The variables existing within the instance are shown together with
477 their values. Instance Variables are used to hold state information for each
478 instance. There will be a group of instance variables for each
479 instance.</p></dd>
480
481 </dl>
482
483 </div></p>
484
485 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
486
487 <a name="8"></a>
488
489 <hr>
490
491 <h2>8&nbsp; Methods</h2>
492
493 <hr>
494
495 <p>There have been a few of the messages and methods in XLISP shown to this
496 point like <a href="../reference/keyword-new.htm">:new</a> and
497 <a href="../reference/keyword-show.htm">:show</a>. The following are the
498 methods built into XLISP:</p>
499
500 <p><div class="box">
501
502 <dl>
503
504 <dt><p><b>:answer</b></p></dt>
505
506 <dd><p>The <a href="../reference/keyword-answer.htm">:answer</a> method
507 allows you to define or change methods within a class.</p></dd>
508
509 <dt><p><b>:class</b></p></dt>
510
511 <dd><p>The <a href="../reference/keyword-class.htm">:class</a> method
512 returns the class of an object.</p></dd>
513
514 <dt><p><b>:isnew</b></p></dt>
515
516 <dd><p>The <a href="../reference/keyword-.htm">:isnew</a> method causes an
517 instance to run its initialization code. When the
518 <a href="../reference/keyword-.htm">:isnew</a> method is run on a class, it
519 resets the class state. This allows you to <nobr>re-define</nobr> instance
520 variables, class <nobr>variables, etc.</nobr></p></dd>
521
522 <dt><p><b>:new</b></p></dt>
523
524 <dd><p>The <a href="../reference/keyword-new.htm">:new</a> method allows you
525 to create an instance when the
526 <a href="../reference/keyword-new.htm">:new</a> message is sent to a
527 <nobr>user-defined</nobr> class. The
528 <a href="../reference/keyword-new.htm">:new</a> method allows you to create
529 a new class when the <a href="../reference/keyword-new.htm">:new</a> message
530 is sent to the
531 <nobr>built-in <a href="../reference/class.htm">class</a></nobr>.</p></dd>
532
533 <dt><p><b>:show</b></p></dt>
534
535 <dd><p>The <a href="../reference/keyword-show.htm">:show</a> method displays the instance or class.</p></dd>
536
537 <dt><p><b>:isa</b></p></dt>
538
539 <dd><p>The <a href="../reference/keyword-isa.htm">:isa</a> method tests if
540 an object inherits from a class.</p></dd>
541
542 </dl>
543
544 </div></p>
545
546 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
547
548 <a name="9"></a>
549
550 <hr>
551
552 <h2>9&nbsp; Sending Messages To A Superclass</h2>
553
554 <hr>
555
556 <p> In addition to the <a href="../reference/send.htm">send</a> function,
557 there is another function called
558 <nobr><a href="../reference/send-super.htm">send-super</a></nobr>. The
559 <nobr><a href="../reference/send-super.htm">send-super</a></nobr>
560 function causes the specified message to be performed by the superclass
561 method. This is a mechanism to allow chaining of methods in a class
562 hierarchy. This chaining behavior can be achieved by creating a method for a
563 class with the <a href="../reference/keyword-answer.htm">:answer</a>
564 message. Within the body of the method, you include a
565 <nobr><a href="../reference/send-super.htm">send-super</a></nobr> form. This
566 function is allowed only inside the execution of a method of an object.</p>
567
568 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
569
570 <a name="10"></a>
571
572 <hr>
573
574 <h2>10&nbsp; Object And Class</h2>
575
576 <hr>
577
578 <p>The definition of the built-in class 'object' is:</p>
579
580 <pre class="example">
581 &gt; (send object :show)
582 Object is #&lt;Object: #[<font color="#0066CC">built-in-object</font>]&gt;, Class is #&lt;Object: #[<font color="#0066CC">built-in-class</font>]&gt;
583 MESSAGES = ((:ISA . #&lt;Subr-: #[<font color="#0000CC">built-in-isa-method</font>]&gt;)
584 (:SHOW . #&lt;Subr-: #[<font color="#0000CC">built-in-show-method</font>]&gt;)
585 (:CLASS . #&lt;Subr-: #[<font color="#0000CC">built-in-class-method</font>]&gt;)
586 (:ISNEW . #&lt;Subr-: #[<font color="#0000CC">built-in-isnew-method</font>]&gt;))
587 IVARS = NIL
588 CVARS = NIL
589 CVALS = NIL
590 SUPERCLASS = NIL <font color="#008844">; no superclass</font>
591 IVARCNT = 0
592 IVARTOTAL = 0
593 #&lt;Object: #[<font color="#0066CC">built-in-object</font>]&gt;
594 </pre>
595
596 <p> Note that 'object' is a class, as opposed to an 'instance-style' object.
597 'object' has no superclass, it is the top or root of the class hierarchy.
598 'object's class is 'class'. </p>
599
600 <pre class="example">
601 &gt; (send class :show)
602 Object is #&lt;Object: #[<font color="#0066CC">built-in-class</font>]&gt;, Class is #&lt;Object: #[<font color="#0066CC">built-in-class</font>]&gt;
603 MESSAGES = ((:ANSWER . #&lt;Subr-: #[<font color="#0000CC">built-in-answer-method</font>]&gt;)
604 (:ISNEW . #&lt;Subr-: #[<font color="#0000CC">built-in-isnew-method</font>]&gt;)
605 (:NEW . #&lt;Subr-: #[<font color="#0000CC">built-in-new-method</font>]&gt;))
606 IVARS = (MESSAGES IVARS CVARS CVALS SUPERCLASS IVARCNT IVARTOTAL)
607 CVARS = NIL
608 CVALS = NIL
609 SUPERCLASS = #&lt;Object: #[<font color="#0066CC">built-in-object</font>]&gt;
610 IVARCNT = 7
611 IVARTOTAL = 7
612 #&lt;Object: #[<font color="#0066CC">built-in-class</font>]&gt;
613 </pre>
614
615 <p>'class' has a superclass of 'object'. It's class is itself, 'class'.</p>
616
617 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
618
619 <a name="11"></a>
620
621 <hr>
622
623 <h2>11&nbsp; Objects Example</h2>
624
625 <hr>
626
627 <p> The following is an example, using the idea of tools again. <nobr>It
628 contains</nobr> a hierarchy of tool classes. <nobr>The top</nobr> of the
629 class hierarchy is 'tools'. '<nobr>hand-tools</nobr>' and
630 '<nobr>shop-tools</nobr>' are <nobr>sub-classes</nobr> of 'tools'. The
631 example creates instances of these <nobr>sub-classes</nobr>. <nobr>It
632 is</nobr> possible to extend this example in various ways. One obvious
633 extension would be to create a third tier of classes under
634 '<nobr>hand-tools</nobr>' that could contain classes like drills,
635 screwdrivers, pliers and <nobr>so on</nobr>. </p>
636
637 <pre class="example">
638 <font color="#008844">;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</font>
639 <font color="#008844">;;</font>
640 <font color="#008844">;; Define the superclasses and classes</font>
641 <font color="#008844">;;</font>
642 <font color="#008844">;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</font>
643 <font color="#008844">;;</font>
644 <font color="#008844">;; make TOOLS superclass</font>
645 <font color="#008844">;;</font>
646 <font color="#008844">;; with a different :ISNEW method</font>
647 <font color="#008844">;; added methods are :BORROW and :RETURN</font>
648 <font color="#008844">;;</font>
649 <font color="#008844">;; class variables NUMBER contains # of tool instances</font>
650 <font color="#008844">;; ACTIVE-LIST contains list of current objects</font>
651 <font color="#008844">;;</font>
652 <font color="#008844">;; instance variables POWER list - (AC BATTERY HAND)</font>
653 <font color="#008844">;; MOVEABLE CAN-CARRY or CAN-ROLL or FIXED</font>
654 <font color="#008844">;; OPERATIONS list</font>
655 <font color="#008844">;; MATERIAL list - (WOOD METAL PLASTIC ...)</font>
656 <font color="#008844">;; PIECES list</font>
657 <font color="#008844">;; LOCATION HOME or person's name</font>
658 <font color="#008844">;;</font>
659
660 (setq tools (send class :new '(power moveable operations
661 material pieces location)
662 '(number active-list)))
663
664 (send tools :answer :isnew '()
665 '((setq number (if (null number) 1 (1+ number))
666 active-list (cons self active-list)
667 location 'home)))
668
669 (send tools :answer :borrow '(by-who)
670 '((if (eq location 'home)
671 (setq location by-who)
672 (print <font color="#880000">"you can't"</font>))))
673
674 (send tools :answer :return '()
675 '((if (eq location 'home)
676 (print <font color="#880000">"got it already"</font>)
677 (setq location 'home))))
678
679 <font color="#008844">;; make HAND-TOOLS class</font>
680 <font color="#008844">;; - with a different :ISNEW method</font>
681 <font color="#008844">;; - new instance variable WEIGHT = &lt;number&gt; of pounds</font>
682 <font color="#008844">;; - the rest is inherited from TOOLS</font>
683
684 (setq hand-tools (send class :new '(weight) '() tools))
685
686 (send hand-tools :answer :isnew '(pow op mat parts w-in)
687 '((setq power pow
688 moveable 'can-carry
689 operations op
690 material mat
691 pieces parts
692 weight w-in)
693 (send-super :isnew)))
694
695 <font color="#008844">;; make SHOP-TOOLS class</font>
696 <font color="#008844">;; - with a different :ISNEW method</font>
697 <font color="#008844">;; - no new instance variables</font>
698 <font color="#008844">;; - the rest is inherited from TOOLS</font>
699
700 (setq shop-tools (send class :new '() '() tools))
701
702 (send shop-tools :answer :isnew '(pow mov op mat parts)
703 '((setq power pow
704 moveable mov
705 operations op
706 material mat
707 pieces parts)
708 (send-super :isnew)))
709
710 <font color="#008844">;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</font>
711 <font color="#008844">;;</font>
712 <font color="#008844">;; Create instances of various tool classes</font>
713 <font color="#008844">;;</font>
714 <font color="#008844">;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</font>
715
716 (setq hand-drill (send hand-tools :new <font color="#008844">; make an instance - HAND-DRILL</font>
717 '(ac)
718 '(drill polish grind screw)
719 '(wood metal plastic)
720 '(drill drill-bits screw-bits buffer)
721 '2.5))
722
723 (setq table-saw (send shop-tools :new <font color="#008844">; make an instance - TABLE-SAW</font>
724 '(ac)
725 'fixed
726 '(rip cross-cut)
727 '(wood plastic)
728 '(saw blades fence)))
729
730 (setq radial-arm (send shop-tools :new <font color="#008844">; make an instance = RADIAL-ARM</font>
731 '(ac)
732 'can-roll
733 '(rip cross-cut)
734 '(wood plastic)
735 '(saw blades dust-bag)))
736 </pre>
737
738 <p>The following session shows how to use the tool definitions from the
739 code above:</p>
740
741 <pre class="example">
742 &gt; (send hand-drill :borrow 'fred)
743 FRED
744
745 &gt; (send table-saw :return)
746 "got it already"
747 "got it already"
748
749 &gt; (send hand-drill :borrow 'joe)
750 "you can't"
751 "you can't"
752
753 &gt; (send hand-drill :return)
754 HOME
755 </pre>
756
757 <p>Fred was able to borrow the '<nobr>hand-drill</nobr>'. When an attempt
758 was made to return the '<nobr>table-saw</nobr>', it was already at home.
759 <nobr>A second</nobr> attempt to borrow the '<nobr>hand-drill</nobr>'
760 indicated that <nobr>&quot;you can't&quot;</nobr> because it was already
761 <nobr>lent out</nobr>. Lastly, the '<nobr>hand-drill</nobr>' was returned
762 successfully. <nobr>[Note that</nobr> the <nobr>&quot;got it
763 already&quot;</nobr> and <nobr>&quot;you can't&quot;</nobr> strings show up
764 twice in the display because the methods both print and return the
765 string.)</p>
766
767 <p>The following example shows the structure of the 'tools' object with the
768 XLISP #ID numbers replaced by the related class and method names:</p>
769
770 <pre class="example">
771 &gt; (send tools :show)
772 Object is #&lt;Object: #[<font color="#0066CC">tools</font>]&gt;, Class is #&lt;Object: #[<font color="#0066CC">class</font>]&gt;
773 MESSAGES = ((:RETURN . #&lt;Closure-:RETURN: #[<font color="#0000CC">tools-return-method</font>]&gt;)
774 (:BORROW . #&lt;Closure-:BORROW: #[<font color="#0000CC">tools-borrow-method</font>]&gt;)
775 (:ISNEW . #&lt;Closure-:ISNEW: #[<font color="#0000CC">tools-isnew-method</font>]&gt;))
776 IVARS = (POWER MOVEABLE OPERATIONS MATERIAL PIECES LOCATION)
777 CVARS = (NUMBER ACTIVE-LIST)
778 CVALS = #(3 (#&lt;Object: #[<font color="#0066CC">radial-arm</font>]&gt;
779 #&lt;Object: #[<font color="#0066CC">table-saw</font>]&gt;
780 #&lt;Object: #[<font color="#0066CC">hand-drill</font>]&gt;))
781 SUPERCLASS = #&lt;Object: #[<font color="#0066CC">object</font>]&gt;
782 IVARCNT = 6
783 IVARTOTAL = 6
784 #&lt;Object: #[<font color="#0066CC">tools</font>]&gt;
785 </pre>
786
787 <p>The two 'tools' sub-classes 'hand-tools' and 'shop-tools' structure looks
788 like:</p>
789
790 <pre class="example">
791 &gt; (send hand-tools :show)
792 Object is #&lt;Object: #[<font color="#0066CC">hand-tools</font>]&gt;, Class is #&lt;Object: #[<font color="#0066CC">class</font>]&gt;
793 MESSAGES = ((:ISNEW . #&lt;Closure-:ISNEW: #[<font color="#0000CC">hand-tools-isnew-method</font>]&gt;))
794 IVARS = (WEIGHT)
795 CVARS = NIL
796 CVALS = NIL
797 SUPERCLASS = #&lt;Object: #[<font color="#0066CC">tools</font>]&gt;
798 IVARCNT = 1
799 IVARTOTAL = 7
800 #&lt;Object: #[<font color="#0066CC">hand-tools</font>]&gt;
801
802 &gt; (send shop-tools :show)
803 Object is #&lt;Object: #[<font color="#0066CC">shop-tools</font>]&gt;, Class is #&lt;Object: #[<font color="#0066CC">class</font>]&gt;
804 MESSAGES = ((:ISNEW . #&lt;Closure-:ISNEW: #[<font color="#0000CC">shop-tools-isnew-method</font>]&gt;))
805 IVARS = NIL
806 CVARS = NIL
807 CVALS = NIL
808 SUPERCLASS = #&lt;Object: #[<font color="#0066CC">tools</font>]&gt;
809 IVARCNT = 0
810 IVARTOTAL = 6
811 #&lt;Object: #[<font color="#0066CC">shop-tools</font>]&gt;
812 </pre>
813
814 <p>The class 'hand-tools' has an instance 'hand-drill' which looks like:</p>
815
816 <pre class="example">
817 &gt; (send hand-drill :show)
818 Object is #&lt;Object: #[<font color="#0066CC">hand-drill</font>]&gt;, Class is #&lt;Object: #[<font color="#0066CC">hand-tools</font>]&gt;
819 WEIGHT = 2.5
820 POWER = (AC)
821 MOVEABLE = CAN-CARRY
822 OPERATIONS = (DRILL POLISH GRIND SCREW)
823 MATERIAL = (WOOD METAL PLASTIC)
824 PIECES = (DRILL DRILL-BITS SCREW-BITS BUFFER)
825 LOCATION = HOME
826 #&lt;Object: #[<font color="#0066CC">hand-drill</font>]&gt;
827 </pre>
828
829 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
830
831 <hr>
832
833 <a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
834 <a href="../manual/contents.htm">Contents</a> |
835 <a href="tutorials.htm">Tutorials</a> |
836 <a href="../examples/examples.htm">Examples</a> |
837 <a href="../reference/reference-index.htm">Reference</a>
838
839 </body></html>