00001 ---- Copyright 2012 Simon Dales
00002 --
00003 -- This work may be distributed and/or modified under the
00004 -- conditions of the LaTeX Project Public License, either version 1.3
00005 -- of this license or (at your option) any later version.
00006 -- The latest version of this license is in
00007 -- http:
00008 --
00009 -- This work has the LPPL maintenance status `maintained'.
00010 --
00011 -- The Current Maintainer of this work is Simon Dales.
00012 --
00013
00014 --[[!
00015 \file
00016 \brief test some classes
00017
00018 ]]
00019
00020 require 'class'
00021
00022 --! \brief write to stdout
00023 function TIO_write(Str)
00024 if Str then
00025 io.write(Str)
00026 end
00027 end
00028
00029 --! \brief writeln to stdout
00030 function TIO_writeln(Str)
00031 if Str then
00032 io.write(Str)
00033 end
00034 io.write('\n')
00035 end
00036
00037 --! \class Animal
00038 --! \brief a base class
00039 Animal = class()
00040
00041 --! \brief constructor
00042 function Animal.init(this)
00043 this:setKind('animal')
00044 end
00045
00046 --! \brief set kind of object
00047 function Animal.setKind(this,Kind)
00048 this.kind = Kind
00049 end
00050
00051 --! \brief say the call of this animal
00052 function Animal.call(this)
00053 local speigel = this.speigel
00054 if speigel then
00055 speigel = ' says "' .. speigel .. '"'
00056 else
00057 speigel = ' <undefined call>'
00058 end
00059
00060 TIO_writeln(this.kind .. speigel)
00061 end
00062
00063 --! \brief an abstract bird
00064 Bird = class(Animal)
00065
00066 --! \brief constructor
00067 function Bird.init(this)
00068 Animal.init(this)
00069 this:setKind('bird')
00070 end
00071
00072 --! \brief a subclassed bird
00073 Pigeon = class(Bird)
00074
00075 --! \brief constructor
00076 function Pigeon.init(this)
00077 Bird.init(this)
00078 this:setKind('pigeon')
00079 this.speigel = 'oh my poor toe Betty'
00080 end
00081
00082 --! \brief another subclassed bird
00083 RedKite = class(Bird)
00084
00085 --! \brief constructor
00086 function RedKite.init(this)
00087 Bird.init(this)
00088 this:setKind('red kite')
00089 this.speigel = 'weee-ooo ee oo ee oo ee oo'
00090 end
00091
00092 --! \brief a base mammal
00093 Mammal = class(Animal)
00094
00095 --! \brief constructor
00096 function Mammal.init(this)
00097 Animal.init(this)
00098 end
00099
00100 --! \brief a subclassed mammal
00101 Cat = class(Mammal)
00102
00103 --! \brief constructor
00104 function Cat.init(this)
00105 Mammal.init(this)
00106 this:setKind('cat')
00107 this.speigel = 'meow'
00108 end
00109
00110 --! \brief another subclassed mammal
00111 Dog = class(Mammal)
00112
00113 --! \brief constructor
00114 function Dog.init(this)
00115 Mammal.init(this)
00116 this:setKind('dog')
00117 this.speigel = 'woof'
00118 end
00119
00120 --eof