home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / xl21hos2.zip / EXAMPLE.LSP < prev    next >
Lisp/Scheme  |  1995-12-27  |  2KB  |  54 lines

  1. #-:classes (load "classes")
  2.  
  3. ; Make the class ship and its instance variables be known
  4.  
  5. (defclass ship ((x 0) (y 0) (xv 0) (yv 0) (mass 0) (name 'unknown) 
  6.     (captain 'unknown) (registry 'unknown)))
  7.  
  8. (defmethod ship :sail (time) 
  9.     ; the METHOD for sailing
  10.     (princ (list "sailing for " time " hours\n"))
  11.        ; note that this form is expressed in terms of objects:  "self"
  12.        ; is bound to the object being talked to during the execution
  13.        ; of its message.  It can ask itself to do things.
  14.        (setf (send self :x)
  15.             (+  (send self :x) (* (send self :xv) time)))
  16.        ; This form performs a parallel action to the above, but more
  17.        ; efficiently, and in this instance, more clearly
  18.        (setq y (+ y (* yv time)))
  19.        ; Cute message for return value.  Tee Hee.
  20.        "Sailing, sailing, over the bountiful chow mein...")
  21.  
  22. ; <a SHIP: #12345667> is not terribly instructive.  How about a more
  23. ; informative print routine?
  24.  
  25. (defmethod ship :print () (princ (list
  26.                 "SHIP NAME: " (send self :name) "\n"
  27.                 "REGISTRY: " (send self :registry) "\n"
  28.                 "CAPTAIN IS: " (send self :captain) "\n"
  29.                 "MASS IS: " (send self :mass) " TONNES\n"
  30.                 "CURRENT POSITION IS: " 
  31.                     (send self :x)    " X BY "
  32.                     (send self :y)    " Y\n"
  33.                 "SPEED IS: "
  34.                     (send self :xv)    " XV BY "
  35.                     (send self :yv)    " YV\n") ) )
  36.  
  37.  
  38. ; and an example object.
  39.  
  40. (definst ship Bounty :mass 50 
  41.              :name 'Bounty 
  42.              :registry 'England 
  43.              :captain 'Bligh)
  44.  
  45. (send Bounty :print)
  46.  
  47. (definst ship lollipop :mass (+ 10 20) :captain 'Temple :x 1000 :y 2000)
  48.  
  49. (send lollipop :print)
  50.  
  51. (definst ship hard :mass 1000 :captain 'Bozo :registry 'North-pole )
  52.  
  53. (send hard :print)
  54.