Sample Code: animals.lua Source File

Sample Code 0.1 20120518

- Main Page
- Classes
- Files

- File List
- File Members

animals.lua
Go to the documentation of this file.
1 ---- Copyright 2012 Simon Dales
2 --
3 -- This work may be distributed and/or modified under the
4 -- conditions of the LaTeX Project Public License, either version 1.3
5 -- of this license or (at your option) any later version.
6 -- The latest version of this license is in
7 -- http://www.latex-project.org/lppl.txt
8 --
9 -- This work has the LPPL maintenance status `maintained'.
10 --
11 -- The Current Maintainer of this work is Simon Dales.
12 --
13
14 --[[!
15 \file
16 \brief test some classes
17
18 ]]
19
20 require 'class'
21
22 --! \brief write to stdout
23 function TIO_write(Str)
24 if Str then
25 io.write(Str)
26 end
27 end
28
29 --! \brief writeln to stdout
30 function TIO_writeln(Str)
31 if Str then
32 io.write(Str)
33 end
34 io.write('\n')
35 end
36
37 --! \class Animal
38 --! \brief a base class
39 Animal = class()
40
41 --! \brief constructor
42 function Animal.init(this)
43 this:setKind('animal')
44 end
45
46 --! \brief set kind of object
47 function Animal.setKind(this,Kind)
48 this.kind = Kind
49 end
50
51 --! \brief say the call of this animal
52 function Animal.call(this)
53 local speigel = this.speigel
54 if speigel then
55 speigel = ' says "' .. speigel .. '"'
56 else
57 speigel = ' <undefined call>'
58 end
59
60 TIO_writeln(this.kind .. speigel)
61 end
62
63 --! \brief an abstract bird
64 Bird = class(Animal)
65
66 --! \brief constructor
67 function Bird.init(this)
68 Animal.init(this)
69 this:setKind('bird')
70 end
71
72 --! \brief a subclassed bird
73 Pigeon = class(Bird)
74
75 --! \brief constructor
76 function Pigeon.init(this)
77 Bird.init(this)
78 this:setKind('pigeon')
79 this.speigel = 'oh my poor toe Betty'
80 end
81
82 --! \brief another subclassed bird
83 RedKite = class(Bird)
84
85 --! \brief constructor
86 function RedKite.init(this)
87 Bird.init(this)
88 this:setKind('red kite')
89 this.speigel = 'weee-ooo ee oo ee oo ee oo'
90 end
91
92 --! \brief a base mammal
93 Mammal = class(Animal)
94
95 --! \brief constructor
96 function Mammal.init(this)
97 Animal.init(this)
98 end
99
100 --! \brief a subclassed mammal
101 Cat = class(Mammal)
102
103 --! \brief constructor
104 function Cat.init(this)
105 Mammal.init(this)
106 this:setKind('cat')
107 this.speigel = 'meow'
108 end
109
110 --! \brief another subclassed mammal
111 Dog = class(Mammal)
112
113 --! \brief constructor
114 function Dog.init(this)
115 Mammal.init(this)
116 this:setKind('dog')
117 this.speigel = 'woof'
118 end
119
120 --eof