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