home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / multiple < prev    next >
Text File  |  2020-01-01  |  6KB  |  204 lines

  1. ; From: Dat Thuc Nguyen
  2. ; Date: Tue, 27 Apr 1999 20:35:16 +0000
  3. ; Subject: Script: Multiple Inheritance in C-Kermit OOP
  4. ; URL: http://www.smalltickle.com
  5. ;
  6. ; Multiple inheritance enriches software design.
  7. ; Not all OOP languages have it: C++ does; Java, Smalltalk don't.
  8. ; The following script implements multiple inheritance in C-Kermit.
  9. ; The famous animal class found in many C++ and Smalltalk references
  10. ; is used to present the subject.
  11. ;
  12. ; The Mammal class of animal has three data members: age, color, kind;
  13. ; and three member functions: kind, age, color, and destroy.
  14. ;
  15. ; The Endangered class of animal has one member data: category, and
  16. ; two member functions: category and destroy.
  17. ;
  18. ; The Bear class of animal derives from the Mammal class.  It overrides
  19. ; the member function: destroy, and supplies one more member function:
  20. ; favorite_food.
  21. ;
  22. ; The Panda class of animal is a multiple inheritance of the Bear and
  23. ; the Endangered classes, it overrides the function: destroy.
  24. ;
  25. ; The proper usage of the following elements in C-Kermit:
  26. ;    assign, _assign, define, _define, \v(macro)
  27. ; creates the OOP mechanisms:
  28. ;    polymorphism, overriding method, late binding.
  29. ;
  30. ; The syntax of Smalltalk in message invocation is applied here to
  31. ; reduce code and simplify class evolution.
  32. ;
  33. ; In the multiple derived class, the selection of the member functions
  34. ; of the super classes can be determined or overridden. This flexibility
  35. ; does not even exist in any of the OOP languages mentioned above.
  36.  
  37. ;**************************************************
  38. ;*   DEFINITION OF THE CLASS MAMMAL               *
  39. ;**************************************************
  40. define Mammal {
  41. ; \%1 name of the new Mammal
  42. ; \%2 age of the new Mammal
  43. ; \%3 color of the new Mammal
  44. ; kind is supplied by the derived class
  45.      _assign Mammal::\%1.age \%2
  46.      _assign Mammal::\%1.color \%3
  47.      _define \%1 {
  48.           if define \m(Mammal::\%1) {
  49.                Mammal::\%1 \v(macro) {\%2} {\%3} {\%4}
  50.           } else {
  51.                return doesNotUnderstand
  52.           }
  53.      }
  54. }
  55.  
  56. ;**************************************************
  57. ;*   PUBLIC USAGE INTERFACE OF THE CLASS MAMMAL   *
  58. ;**************************************************
  59. define Mammal::kind {
  60.      echo \m(Mammal::\%1.kind)
  61. }
  62. define Mammal::age {
  63.      echo \m(Mammal::\%1.age)
  64. }
  65. define Mammal::color {
  66.      echo \m(Mammal::\%1.color)
  67. }
  68. define Mammal::destroy {
  69.      _define Mammal::\%1.kind
  70.      _define Mammal::\%1.age
  71.      _define Mammal::\%1.color
  72.      _define \%1
  73. }
  74.  
  75. ;**************************************************
  76. ;*   DEFINITION OF THE CLASS BEAR                 *
  77. ;**************************************************
  78. define Bear {
  79. ; \%1 name of the new Bear
  80. ; \%2 age of the new Bear
  81. ; \%3 color of the new Bear
  82.      Mammal Mammal::\%1 \%2 \%3
  83.      _assign Mammal::Mammal::\%1.kind \v(macro)
  84.      _define \%1 {
  85.           local \%r
  86.           if define \m(Bear::\%1) {
  87.                Bear::\%1 \v(macro) \%2 \%3
  88.                if FAIL END \v(return)
  89.                assign \%r \v(return)
  90.           } else {
  91.                Mammal::\v(macro) \%1 \%2 \%3
  92.                if FAIL END \v(return)
  93.                assign \%r \v(return)
  94.           }
  95.           return \%r
  96.      }
  97. }
  98.  
  99. ;**************************************************
  100. ;*   PUBLIC USAGE INTERFACE OF THE CLASS BEAR     *
  101. ;**************************************************
  102. define Bear::favorite_food {
  103.      echo Honey
  104. }
  105. define Bear::destroy {
  106.      Mammal::\%1 destroy
  107.      _define \%1
  108. }
  109.  
  110. ;**************************************************
  111. ;*   DEFINITION OF THE CLASS ENDANGERED           *
  112. ;**************************************************
  113. define Endangered {
  114. ; \%1 Endangered name
  115. ; \%2 endangered category
  116.      _assign Endangered::\%1.category \%2
  117.      _define \%1 {
  118.           if define \m(Endangered::\%1) {
  119.                Endangered::\%1 \v(macro) {\%2} {\%3} {\%4}
  120.           } else {
  121.                return doesNotUnderstand
  122.           }
  123.      }
  124. }
  125.  
  126. ;****************************************************
  127. ;*   PUBLIC USAGE INTERFACE OF THE CLASS ENDANGERED *
  128. ;****************************************************
  129. define Endangered::category {
  130.      echo \m(Endangered::\%1.category)
  131. }
  132. define Endangered::destroy {
  133.      _define Endangered::\%1.category
  134.      _define \%1
  135. }
  136.  
  137. ;**************************************************
  138. ;*   DEFINITION OF THE CLASS PANDA                *
  139. ;**************************************************
  140. define Panda {
  141. ; \%1 name of the new Panda
  142. ; \%2 age of the new Panda
  143. ; \%3 endangered category of the new Panda
  144.      Bear Bear::\%1 \%2 black_and_white
  145.      Endangered Endangered::\%1 \%3
  146.      _assign Mammal::Mammal::Bear::\%1.kind \v(macro)
  147.      _define \%1 {
  148.           local \%r super \%i \&a[]
  149.           if define \m(Panda::\%1) {
  150.                Panda::\%1 \v(macro) \%2 \%3 \%4
  151.                if FAIL END \v(return)
  152.                assign \%r \v(return)
  153.           } else {
  154.                assign super Bear:Endangered
  155.                for \%i 1 \fsplit(\m(super),&a,:) 1 {
  156.                     \&a[\%i]::\v(macro) \%1 \%2 \%3
  157.                     if FAIL END \v(return)
  158.                     assign \%r \v(return)
  159.                     if define \%r {
  160.                          if NOT EQUAL \%r doesNotUnderstand return \%r
  161.                     }
  162.                }
  163.           }
  164.           return \%r
  165.      }
  166. }
  167.  
  168. ;**************************************************
  169. ;*   PUBLIC USAGE INTERFACE OF THE CLASS PANDA    *
  170. ;**************************************************
  171. define Panda::destroy {
  172.      _define Bear::\%1
  173.      _define Endangered::\%1
  174.      _define \%1
  175. }
  176.  
  177. ;**************************************************
  178. ;    USAGE SAMPLES                                *
  179. ;**************************************************
  180.  
  181. ; BEJING presented the Zoo of NEWYORK with a Panda
  182. ; named Teddy, 3 years old, endangered category 5,
  183. ; its default color is naturally black_and_white.
  184.  
  185. Panda Teddy 3 5
  186.  
  187. ; What kind of animal is Teddy?
  188. Teddy kind          ; => panda
  189.  
  190. ; How old is Teddy?
  191. Teddy age           ; => 3
  192.  
  193. ; What endangered category is Teddy?
  194. Teddy category      ; => 5
  195.  
  196. ; What is Teddy's favorite food?
  197. Teddy favorite_food ; => honey
  198.  
  199. ; Yesterday somebody stole Teddy, the Zoo administration
  200. ; deletes him from the zoo habitant list:
  201. Teddy destroy
  202.  
  203. END
  204.