home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / languages / scheme / xscheme028 / doc / OO-Primer < prev    next >
Encoding:
Text File  |  1992-04-27  |  30.9 KB  |  728 lines

  1.  
  2.  
  3.  
  4.  
  5.                          XSCHEME 0.28 OBJECTS PRIMER
  6.                                February 23, 1992
  7.  
  8.                   derived from the XLISP 2.0 OBJECTS PRIMER
  9.                 written by Tim I Mikkelsen on February 3, 1990
  10.  
  11.  
  12.  
  13.  
  14.         Original copyright notice:
  15.  
  16.         Copyright  (c) 1990 by Tim I.  Mikkelsen.  All Rights  Reserved.
  17.         No part of this document may be copied, reproduced or translated
  18.         for commercial use without prior written  consent of the author.
  19.         Permission  is granted  for  non-commercial  use as long as this
  20.         notice is left intact.
  21.  
  22.         Mine:
  23.         Do what you like with it as long as  it  doesn't  conflict  with
  24.         Tim's copyright notice.
  25.         ________________________________________________________________
  26.  
  27.  
  28.         One of the  features in the design of XSCHEME is object-oriented
  29.         programming.  This  primer is  intended to serve as a very brief
  30.         introduction  to  the  object  facilities  of  the  XSCHEME 0.28
  31.         dialect of SCHEME.  Note that the object features of XSCHEME are
  32.         not based on  other existing object definitions  in other SCHEME
  33.         dialects.  If  you  find problems in  the primer, I'd appreciate
  34.         hearing.
  35.  
  36.                              Gunnar Zoetl
  37.                              Talstrasse 32
  38.                              D-6074 Roedermark
  39.                              Germany
  40.  
  41.                      E-Mail: gunnar@fasel.hotb.sub.org
  42.  
  43.         However, note that I have not  written this primer, but modified
  44.         the original version by Tim Mikkelsen to suit XSCHEME instead of
  45.         XLISP.  Some bits of the message descriptions are taken from the
  46.         XLISP 1.5 Manual by D. M. Betz and also modified. If you find it
  47.         good, it's probably Tim's, if it's weird, it might be mine ;-)
  48.  
  49.         Tim's address is:                       David's address is:
  50.              Tim Mikkelsen                         stated on the front
  51.              4316 Picadilly Drive                  page of his manual.
  52.              Fort Collins, Colorado  80526
  53.  
  54.  
  55. PROGRAMMING STYLES
  56. ______________________________________________________________________________
  57.  
  58.  
  59. There are many  programming  paradigms  (models).  Some of the paradigms
  60. are procedural, functional, rule-based, declarative and object-oriented.
  61. A language can have aspects of one or many of these programming  models.
  62.  
  63.  
  64. Procedure-Oriented
  65.  
  66. The programming paradigm most people are familiar with is the procedural
  67. style.  The primitives in procedural  programming  are:  subroutines and
  68. data  structures.  Through  these  primitives,   programmers  have  some
  69. limited abilities to share programs and program fragments.  C and Pascal
  70. are examples of procedural  languages.  Some procedural  languages (such
  71. as Modula and ADA) have  extensions  that provide for better  sharing of
  72. code.
  73.  
  74.  
  75. Object-Oriented Programming
  76.  
  77. Object-oriented  programming  is based  on the  primitives  of  objects,
  78. classes and messages.  Objects are defined in terms of classes.  Actions
  79. occur by sending a message to an object.  An object's  definition can be
  80. inherited  from  more  general  classes.  Objective-C  and C++ both  are
  81. object-oriented  dialects of the C language.  Many dialects of LISP have
  82. some object oriented extension (Flavors, Common LOOPS, CLOS and others).
  83. There  currently is standards  work  proceeding  to add  object-oriented
  84. programming to Common LISP.
  85.  
  86.  
  87. Object Oriented Programming
  88.  
  89. So, the  object-oriented  programming model is based around the concepts
  90. of objects,  classes and messages.  An object is essentially a black box
  91. that contains internal state  information.  You send an object a message
  92. which causes the object to perform some  operation.  Objects are defined
  93. and described through classes.
  94.  
  95. One aspect of an object is that you do not have to know what is inside -
  96. or how it  works - to be able to use it.  From a  programming  point  of
  97. view,  this is very  handy.  You can  develop a series  of  objects  for
  98. someone to use.  If you need to change what goes on inside, the users of
  99. the objects should be unaware.
  100.  
  101. Another aspect of objects is that of  inheritance.  You can build up new
  102. classes  from  existing  classes  by  inheriting  the  existing  class's
  103. functionality  and then extending the new  definition.  For example, you
  104. can  define a tool class  (with  various  attributes)  and then go about
  105. creating  object  instances  tool-1,  tool-2,  and so on.  You can  also
  106. create new sub-classes of the tool class like  power-tool.  This is also
  107. very handy because you don't have to  re-implement  something if you can
  108. build it up from existing code.
  109.  
  110.  
  111.  
  112.  
  113. XSCHEME OBJECT-ORIENTED PROGRAMMING
  114. ______________________________________________________________________________
  115.  
  116.  
  117.  
  118. XSCHEME OBJECT TERMINOLOGY
  119.  
  120. There  are, as  previously  mentioned,  many  different  languages  with
  121. object-oriented  extensions and facilities.  The terminology, operations
  122. and  styles of these are very  different.  Some of the main  definitions
  123. for XSCHEME's object-oriented extensions are:
  124.  
  125.         Object data type        The OBJECT DATA TYPE is a built-in  data
  126.                                 type of  XSCHEME. Members of the  object
  127.                                 data  type  are  object   instances  and
  128.                                 classes.
  129.  
  130.         Object instances        An  OBJECT   INSTANCE  is  a   composite
  131.                                 structure that contains  internal  state
  132.                                 information,  methods  (the  code  which
  133.                                 respond to  messages),  a pointer to the
  134.                                 object  instance's  defining class and a
  135.                                 pointer  to  the  object's  super-class.
  136.                                 XSCHEME  contains   no  built-in  object
  137.                                 instances.
  138.  
  139.         Class objects           A  CLASS  OBJECT  is,  essentially,  the
  140.                                 template for defining the derived object
  141.                                 instances.  A  class  object,   although
  142.                                 used  differently  from a simple  object
  143.                                 instance,  is  structurally  a member of
  144.                                 the  object   data   type.  It  is  also
  145.                                 contains  the  linking   mechanism  that
  146.                                 allows  you to build  class  hierarchies
  147.                                 (sub-classes and super-classes). XSCHEME
  148.                                 contains  two  built-in  class  objects:
  149.                                 OBJECT and CLASS.
  150.  
  151.         Message selector        The MESSAGE  SELECTOR is the symbol that
  152.                                 is used to  select a  particular  action
  153.                                 (Method) from the object.
  154.  
  155.         Message                 The  MESSAGE is the  combination  of the
  156.                                 message  selector  and the data (if any)
  157.                                 to be sent to the object.
  158.  
  159.         Method                  The METHOD is the actual  code that gets
  160.                                 executed  when the object  receives  the
  161.                                 Message.
  162.  
  163.  
  164.  
  165. SENDING MESSAGES
  166.  
  167. Message sending in XSCHEME is done by specifying the destination object,
  168. the  message selector  and  the arguments to be passed  to  the selected
  169. method  in  the  following  way:   (<object> <selector> [<argument>...])
  170. <object>, <selector> and the <argument>s are evaluated.
  171.  
  172. The way that a user  creates a new object is to send a 'NEW message to a
  173. previously  defined  class.  The  result  of this  send will  return  an
  174. object, so  this  is  normally preceded by a SET! or DEFINE.  I will use
  175. SET!  throughout  this  document,  for  the  exact  difference  see  the
  176. "Revised^3  Report on  the  Algorithmic Language Scheme"  or the Xscheme
  177. manual.  The values shown in the examples that follow may not match what
  178. you  see  if  you try this on  your version of XSCHEME - this is not  an
  179. error.  The  screens that are  used  in the various examples are similar
  180. to  what you should see on  your computer screen. The  ">" is the normal
  181. XSCHEME prompt (the characters that follow the prompt is what you should
  182. type in to try these examples).
  183.  
  184.  
  185.          ________________________________________________________________
  186.         |
  187.         |       > (set! my-object (object 'new))
  188.         |       #<Object #48340>
  189.         |________________________________________________________________
  190.  
  191.  
  192. The object  created here is of limited  value.  Most often, you create a
  193. class  object and then you create  instances  of that  class.  So in the
  194. following  example, a class called MY-CLASS is created that inherits its
  195. definition from the a built-in CLASS definition.  Then two instances are
  196. created of the new class.
  197.  
  198.          ________________________________________________________________
  199.         |
  200.         |       > (set! my-class (class 'new '()))
  201.         |       #<Object #47f20>
  202.         |       > (set! my-instance (my-class 'new))
  203.         |       #<Object #47b90>
  204.         |       > (set! another-instance (my-class 'new))
  205.         |       #<Object #4786c>
  206.         |________________________________________________________________
  207.  
  208.  
  209.  
  210. CLASSES
  211.  
  212. Previously,  a 'NEW  message was used to create an object.  The  message
  213. used to see what is in an object is the 'SHOW message.
  214.  
  215.          ________________________________________________________________
  216.         |
  217.         |       > (my-class 'show)
  218.         |       Object is #<Object #47f20>, Class is #<Object #2e4c4>
  219.         |         messages = ()
  220.         |         ivars = (%%class)
  221.         |         cvars = #<Environment #47efc>
  222.         |         superclass = #<Object #2e5b4>
  223.         |         ivarcnt = 0
  224.         |         ivartotal = 0
  225.         |       #<Object #47f20>
  226.         |________________________________________________________________
  227.  
  228.  
  229. From the display of the MY-CLASS  object you can see there are a variety
  230. of components.  The components of a class are:
  231.  
  232.         Class Pointer           "Class is #<Object #2e4c4>"
  233.                                 This  pointer  shows to what  class  the
  234.                                 object (instance or class) belongs.  For
  235.                                 a  class,  this  always  points  to  the
  236.                                 built-in  object  CLASS.  This  is  also
  237.                                 true  of the  CLASS  object,  its  class
  238.                                 pointer points to itself.
  239.  
  240.         Superclass Pointer      "superclass = #<Object #2e5b4>"
  241.                                 This  pointer  shows what the next class
  242.                                 up the class  hierarchy is.  If the user
  243.                                 does  not  specify  what  class  is  the
  244.                                 superclass,   it  will   point   to  the
  245.                                 built-in class OBJECT.
  246.  
  247.         Messages                "messages = ()"
  248.                                 This  component  shows what messages are
  249.                                 allowed   for   the   class,   and   the
  250.                                 description  of the method  that will be
  251.                                 used.  If the method is  system-defined,
  252.                                 it will show up in the form  of '#<Subr
  253.                                 %CLASS-NEW>'. Remember  that  the  class
  254.                                 hierarchy    (through   the   Superclass
  255.                                 Pointer) is  searched  if the  requested
  256.                                 message is not found in the class.
  257.  
  258.         Instance Variables      "ivars = (%%class)"
  259.                                 This   component   lists  what  instance
  260.                                 variables will be created when an object
  261.                                 instance is created.  If no instances of
  262.                                 the class  exist,  there are no Instance
  263.                                 Variables.  If there are 5 instances  of
  264.                                 a  class,  there  are  5  complete   and
  265.                                 different   groups   of   the   Instance
  266.                                 Variables.
  267.  
  268.         Class Variables         "cvars = #<Environment #47efc>"
  269.                                 This   component  shows  the environment
  270.                                 in  which  the class variables exist for
  271.                                 the  given  class.  Class  Variables are
  272.                                 used to  hold  state information about a
  273.                                 class. There will be one of  each of the
  274.                                 Class  Variables,  independent   of  the
  275.                                 number of instances of the class created
  276.  
  277. A BETTER EXAMPLE
  278.  
  279. The  example  previously  shown  does work, but the class and  instances
  280. created  don't really do anything of  interest.  The  following  example
  281. sets up a tool class and creates some tool instances.
  282.  
  283.          ________________________________________________________________
  284.         |
  285.         |       > (set! my-tools (class 'new '(power moveable operation)))
  286.         |       #<Object #48208>
  287.         |
  288.         |       > (my-tools 'answer 'isnew '(pow mov op)
  289.         |                                  '((set! power pow)
  290.         |                                    (set! moveable mov)
  291.         |                                    (set! operation op)
  292.         |                                    self))
  293.         |       #<Object #48208>
  294.         |
  295.         |       > (set! drill (my-tools 'new 'AC t 'holes))
  296.         |       #<Object #46edc>
  297.         |
  298.         |       > (set! hand-saw (my-tools 'new 'none t 'cuts))
  299.         |       #<Object #46594>
  300.         |
  301.         |       > (set! table-saw (my-tools 'new 'AC nil 'cuts))
  302.         |       #<Object #460cc>
  303.         |________________________________________________________________
  304.  
  305.  
  306. So, a class of objects called MY-TOOLS was created.  Note that the class
  307. object  MY-TOOLS was created by sending the 'NEW message to the built-in
  308. CLASS  object.  Within  the  MY-TOOL  class,  there are three  instances
  309. called DRILL, HAND-SAW and TABLE-SAW.  These were created by sending the
  310. 'NEW message to the MY-TOOLS  class object.  Notice that the  parameters
  311. followed the message selector.
  312.  
  313.  
  314. INSTANCES
  315.  
  316. The  following  is a display of the  contents of some of the  previously
  317. created instances:
  318.  
  319.          ________________________________________________________________
  320.         |
  321.         |       > (drill 'show)
  322.         |       Object is #<Object #46edc>, Class is #<Object #48208>
  323.         |         power = ac
  324.         |         moveable = #t
  325.         |         operation = holes
  326.         |       #<Object #46edc>
  327.         |
  328.         |       > (hand-saw 'show)
  329.         |       Object is #<Object #46594>, Class is #<Object #48208>
  330.         |         power = none
  331.         |         moveable = #t
  332.         |         operation = cuts
  333.         |       #<Object #46594>
  334.         |________________________________________________________________
  335.  
  336.  
  337. From  the  display  of  these  instances  you  can see  there  are  some
  338. components and values.  The components of an instance are:
  339.  
  340.         Class Pointer           "Class is #<Object #12345>"
  341.                                 This  pointer  shows to which  class the
  342.                                 current object instance  belongs.  It is
  343.                                 through this link that the system  finds
  344.                                 the methods to execute for the  received
  345.                                 messages.
  346.  
  347.         Instance Variables      "power = none ..."
  348.         and Values              The Instance  Variables (IVAR) component
  349.                                 lists what  variables  exist  within the
  350.                                 instance.  The Instance Values component
  351.                                 holds  what the  current  values  of the
  352.                                 variables  are.  Instance  Variables are
  353.                                 used to hold state  information for each
  354.                                 instance.  There  will  be  a  group  of
  355.                                 Instance Variables for each instance.
  356.  
  357.  
  358.  
  359.  
  360. METHODS
  361.  
  362. There have been a few of the messages and methods in XSCHEME shown to this
  363. point ('NEW and 'SHOW).  The following are the methods built into XSCHEME:
  364.  
  365.         'answer <msg> <fargs> <code>
  366.                         where:
  367.                         <msg>       the message symbol
  368.                         <fargs>     the formal argument list
  369.                                       this list is of the form:
  370.                                         ([<farg>]...
  371.                                          [#!optional [<oarg>]...]
  372.                                          [#!rest <rarg>]
  373.                                          [#!aux [<aux>]...])
  374.                                       where
  375.                                         <farg>   a formal argument
  376.                                         <oarg>   an optional argument
  377.                                                  (default is nil)
  378.                                         <rarg>   bound to the rest of
  379.                                                  the arguments
  380.                                         <aux>    a auxiliary variable
  381.                                                  (set to nil)
  382.                         <code>      a list of executable expressions
  383.                         The  'ANSWER  method  allows  you to  define  or
  384.                         change methods within a class.
  385.                         returns: the object.
  386.  
  387.  
  388.         'isnew <ivars> [<cvars>[<super>]]
  389.                         where:
  390.                         <ivars>     the list of instance variables
  391.                         <cvars>     the list of class variables (default is nil)
  392.  
  393.  
  394.                         <super>     the superclass (default is Object)
  395.                         The 'ISNEW method  causes an instance to run its
  396.                         initialization  code.  When the 'ISNEW method is
  397.                         run on a class, it resets the class state.  This
  398.                         allows  you  to  re-define  instance  variables,
  399.                         class variables, etc.
  400.                         returns: the object.
  401.  
  402.         'new <parameters>
  403.                         The 'NEW method allows you to create an instance
  404.                         when the 'NEW message is sent to a  user-defined
  405.                         class.  The 'NEW  method  allows you to create a
  406.                         new class (when the 'NEW  message is sent to the
  407.                         built-in CLASS).
  408.                         When  a  new  instance  of a class is created by
  409.                         sending the message new" to  an  existing class,
  410.                         the   message   "'isnew"  followed  by  whatever
  411.                         parameters were passed to the "'new" message  is
  412.                         sent to  the  newly created object.  When a  new
  413.                         class  is  created by sending the "'new" message
  414.                         to the object "Class", an optional parameter may
  415.                         be  specified indicating  the superclass of  the
  416.                         new  class.  If  this parameter is  omitted, the
  417.                         new class  will  be a  subclass  of  Object".  A
  418.                         class  inherits  all  instance  variables, class
  419.                         variables, and methods from its super-class.
  420.                         returns: the newly defined object
  421.  
  422.         'show
  423.                         The 'SHOW method displays the instance or class.
  424.                         returns: the object.
  425.  
  426.         'class
  427.                         The 'CLASS method returns the class of an object.
  428.                         returns: the object.
  429.  
  430.  
  431. When you redefine one  of  the  methods selected  by these these messages
  432. for your own classes,  which will be most common for the 'isnew  message,
  433. be careful to return the same as the original method does.
  434.  
  435. SENDING MESSAGES TO A SUPERCLASS
  436.  
  437. The SEND-SUPER  function  causes the specified message to be performed by
  438. the  superclass  method.  SEND-SUPER is used in the same way a message is
  439. sent, i.e. (sendsuper <selector> [<argument>...]).  This  is a  mechanism
  440. to  allow  chaining  of  methods  in  a  class hierarchy.  This  chaining
  441. behavior can be achieved by creating  a  method  for  a  class  with  the 
  442. 'ANSWER   message.  Within  the   body  of  the  method,  you  include  a 
  443. SEND-SUPER form.  This function  is allowed only inside the  execution of
  444. a method of an object.
  445.  
  446.  
  447. OBJECT AND CLASS
  448.  
  449. The definition of the built-in class OBJECT is:
  450.  
  451.          ________________________________________________________________
  452.         |
  453.         |       > (object 'show)
  454.         |       Object is #<Object #2e5b4>, Class is #<Object #2e4c4>
  455.         |         messages = ((show . #<Subr %OBJECT-SHOW>)
  456.         |                     (class . #<Subr %OBJECT-CLASS>)
  457.         |                     (isnew . #<Subr %OBJECT-ISNEW>))
  458.         |         ivars = (%%class)
  459.         |         cvars = ()
  460.         |         superclass = ()
  461.         |         ivarcnt = 0
  462.         |         ivartotal = 0
  463.         |       #<Object #2e5b4>
  464.         |________________________________________________________________
  465.  
  466.  
  467. Note that OBJECT is a class - as opposed to an "instance-style"  object.
  468. OBJECT has no superclass, it is the top or root of the class  hierarchy.
  469. OBJECT's class is CLASS.
  470.  
  471.          ________________________________________________________________
  472.         |
  473.         |       > (class 'show)
  474.         |       Object is #<Object #2e4c4>, Class is #<Object #2e4c4>
  475.         |         messages = ((answer . #<Subr %CLASS-ANSWER>)
  476.         |                     (isnew . #<Subr %CLASS-ISNEW>)
  477.         |                     (new . #<Subr %CLASS-NEW>))
  478.         |         ivars = (%%class messages ivars cvars superclass
  479.         |                                         ivarcnt ivartotal)
  480.         |         cvars = ()
  481.         |         superclass = #<Object #2e5b4>
  482.         |         ivarcnt = 6
  483.         |         ivartotal = 6
  484.         |       #<Object #2e4c4>
  485.         |________________________________________________________________
  486.  
  487.  
  488. CLASS has a superclass of OBJECT.  It's class is itself - CLASS.
  489.  
  490.  
  491.  
  492.  
  493. A MORE REALISTIC EXAMPLE
  494. ______________________________________________________________________________
  495.  
  496.  
  497. The following is an example, using the idea of tools again.  It contains
  498. a hierarchy of tool  classes.  The top of the class  hierarchy is TOOLS.
  499. HAND-TOOLS and SHOP-TOOLS are sub-classes of TOOLS.  The example creates
  500. instances of these  sub-classes.  It is possible to extend this  example
  501. in various ways.  One obvious  extension would be to create a third tier
  502. of classes  under  HAND-TOOLS  that could  contain  classes like drills,
  503. screwdrivers, pliers and so on.
  504.  
  505.  
  506. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  507. ;
  508. ;       File name:      tools.s
  509. ;
  510. ;       original XLISP-version:
  511. ;
  512. ;       Author:         Tim Mikkelsen
  513. ;       Description:    Object-oriented example program
  514. ;       Language:       XLISP 2.0
  515. ;
  516. ;       Date Created:   10-Jan-1988
  517. ;       Date Updated:   2-Apr-1989
  518. ;
  519. ;       (c) Copyright 1988, by Tim Mikkelsen, all rights reserved.
  520. ;           Permission is granted for unrestricted non-commercial use.
  521. ;
  522. ;       port to XScheme:
  523. ;
  524. ;       Language:       Xscheme 0.28
  525. ;
  526. ;       Date Created:   23-Feb-1992
  527. ;
  528. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  529.  
  530. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  531. ;
  532. ;       Define the superclasses and classes
  533. ;
  534. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  535.  
  536. ;
  537. ; make TOOLS superclass
  538. ;       with a different 'ISNEW method
  539. ;       added methods are 'BORROW and 'RETURN
  540. ;       class variables are     NUMBER          contains # of tool instances
  541. ;                               ACTIVE-LIST     contains list of current objects
  542.  
  543.  
  544. ;       instance variables are  POWER           list - (AC BATTERY HAND)
  545. ;                               MOVEABLE        CAN-CARRY or CAN-ROLL or FIXED
  546. ;                               OPERATIONS      list
  547. ;                               MATERIAL        list - (WOOD METAL PLASTIC ...)
  548. ;                               PIECES          list
  549. ;                               LOCATION        HOME or person's name
  550. ;
  551.  
  552. (set! tools (class 'new '(power
  553.                           moveable
  554.                           operations
  555.                           material
  556.                           pieces
  557.                           location)
  558.                           '(number active-list)))
  559. (tools 'answer 'isnew '()
  560.                '((if (null? number) (set! number 1)
  561.                                     (set! number (1+ number)))
  562.                      (set! active-list (cons self active-list))
  563.                      (set! location 'home)
  564.                      self))
  565. (tools 'answer 'borrow '(by-who)
  566.                '((if (eq? location 'home) (set! location by-who)
  567.                                           (display "you can't"))))
  568. (tools 'answer 'return '()
  569.                '((if (eq? location 'home) (display "got it already")
  570.                                           (set! location 'home))))
  571.  
  572. ;
  573. ; make HAND-TOOLS class
  574. ;       with a different 'ISNEW method
  575. ;       new instance variable   WEIGHT          <number> of pounds
  576. ;       the rest is inherited from TOOLS
  577. ;
  578.  
  579. (set! hand-tools (class 'new '(weight) '() tools))
  580. (hand-tools 'answer 'isnew '(pow op mat parts w-in)
  581.                            '((set! power pow)
  582.                              (set! moveable 'can-carry)
  583.                              (set! operations op)
  584.                              (set! material mat)
  585.                              (set! pieces parts)
  586.                              (set! weight w-in)
  587.                              (send-super 'isnew)))
  588.  
  589. ;
  590. ; make SHOP-TOOLS class
  591. ;       with a different 'ISNEW method
  592. ;       no new instance variables
  593. ;       the rest is inherited from TOOLS
  594. ;
  595.  
  596. (set! shop-tools (class 'new '() '() tools))
  597. (shop-tools 'answer 'isnew '(pow mov op mat parts)
  598.                            '((set! power pow)
  599.                             (set! moveable mov)
  600.                             (set! operations op)
  601.                             (set! material mat)
  602.                             (set! pieces parts)
  603.                             (send-super 'isnew)
  604.                             self))
  605.  
  606. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  607. ;
  608. ;       Create instances of various tool classes
  609. ;
  610. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  611.  
  612. (set! hand-drill (hand-tools 'new          ; make an instance - HAND-DRILL
  613.                              '(ac)
  614.                              '(drill polish grind screw)
  615.                              '(wood metal plastic)
  616.                              '(drill drill-bits screw-bits buffer)
  617.                              '2.5))
  618.  
  619. (set! table-saw (shop-tools 'new           ; make an instance - TABLE-SAW
  620.                              '(ac)
  621.                              'fixed
  622.                              '(rip cross-cut)
  623.                              '(wood plastic)
  624.                              '(saw blades fence)))
  625.  
  626.  
  627. (set! radial-arm (shop-tools 'new          ; make an instance = RADIAL-ARM
  628.                              '(ac)
  629.                              'can-roll
  630.                              '(rip cross-cut)
  631.                              '(wood plastic)
  632.                              '(saw blades dust-bag)))
  633.  
  634.  
  635.  
  636.  
  637. The following  session shows how to use the tool  definitions  from this
  638. better  example.  The example loads "tools.s" into xscheme.
  639.          ________________________________________________________________
  640.         |
  641.         |       > (load "tools.s")
  642.         |       #t
  643.         |
  644.         |       > (hand-drill 'borrow 'fred)
  645.         |       fred
  646.         |
  647.         |       > (table-saw 'return)
  648.         |       got it already#t
  649.         |
  650.         |       > (hand-drill 'borrow 'joe)
  651.         |       you can't#t
  652.         |
  653.         |       > (hand-drill 'return)
  654.         |       home
  655.         |________________________________________________________________
  656.  
  657.  
  658. So, Fred was able to borrow the HAND-DRILL.  When an attempt was made to
  659. return the  TABLE-SAW,  it was  already  at home.  A second  attempt  to
  660. borrow the HAND-DRILL  indicated that "you can't" because it was already
  661. lent out.  Lastly, the HAND-DRILL was returned successfully.  (Note that
  662. the "got it  already"  and "you  can't"  strings  show up  twice  in the
  663. display because the methods both print and return the string.)
  664.  
  665. The following session shows the structure of the TOOLS object:
  666.  
  667.          ________________________________________________________________
  668.         |
  669.         |       > (tools 'show)
  670.         |       Object is #<Object #477f4>, Class is #<Object #2e4c4>
  671.         |         messages = ((return . #<Method RETURN>)
  672.         |                     (borrow . #<Method BORROW>)
  673.         |                     (isnew . #<Method ISNEW>))
  674.         |         ivars = (%%class power moveable operations material
  675.         |                                             pieces location)
  676.         |         cvars = #<Environment #47758>
  677.         |         superclass = #<Object #2e5b4>
  678.         |         ivarcnt = 6
  679.         |         ivartotal = 6
  680.         |       #<Object #477f4>
  681.         |________________________________________________________________
  682.  
  683.  
  684. The two TOOLS sub-classes HAND-TOOLS and SHOP-TOOLS structure looks like:
  685.  
  686.          ________________________________________________________________
  687.         |
  688.         |       > (hand-tools 'show)
  689.         |       Object is #<Object #45e68>, Class is #<Object #2e4c4>
  690.         |         messages = ((isnew . #<Method ISNEW>))
  691.         |         ivars = (%%class power moveable operations material
  692.         |                                      pieces location weight)
  693.         |         cvars = #<Environment #45e44>
  694.         |         superclass = #<Object #477f4>
  695.         |         ivarcnt = 1
  696.         |         ivartotal = 7
  697.         |       #<Object #45e68>
  698.         |
  699.         |       > (shop-tools 'show)
  700.         |       Object is #<Object #452bc>, Class is #<Object #2e4c4>
  701.         |         messages = ((isnew . #<Method ISNEW>))
  702.         |         ivars = (%%class power moveable operations material
  703.         |                                             pieces location)
  704.         |         cvars = #<Environment #45298>
  705.         |         superclass = #<Object #477f4>
  706.         |         ivarcnt = 0
  707.         |         ivartotal = 6
  708.         |       #<Object #452bc>
  709.         |________________________________________________________________
  710.  
  711.  
  712. The class HAND-TOOLS has an instance HAND-DRILL which looks like:
  713.  
  714.          ________________________________________________________________
  715.         |
  716.         |       > (hand-drill 'show)
  717.         |       Object is #<Object #44464>, Class is #<Object #45e68>
  718.         |         power = (ac)
  719.         |         moveable = can-carry
  720.         |         operations = (drill polish grind screw)
  721.         |         material = (wood metal plastic)
  722.         |         pieces = (drill drill-bits screw-bits buffer)
  723.         |         location = home
  724.         |         weight = 2.5
  725.         |       #<Object #44464>
  726.         |________________________________________________________________
  727.  
  728.