-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoo.lua
216 lines (175 loc) · 3.9 KB
/
oo.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
-- http://lua-users.org/wiki/ObjectOrientationTutorial
-- : syntax sugar
-- call function
-- insert table as self
local MyClass = {}
MyClass.__index = MyClass
function MyClass.new(value)
local obj = setmetatable({}, MyClass)
obj.value = value
return obj
end
function MyClass.set_value(self, newval)
self.value = newval
end
function MyClass.get_value(self)
return self.value
end
print("Myclass ver1:")
local obj = MyClass.new(3)
print(obj:get_value())
obj:set_value(4)
print(obj:get_value())
--
print("My class ver2")
local MyClass = {}
MyClass.__index = MyClass
setmetatable(MyClass, {
__call = function(cls, ...)
return cls.new(...)
end,
})
function MyClass.new(value)
local obj = setmetatable({}, MyClass)
obj.value = value
return obj
end
function MyClass:set_value(newval)
self.value = newval
end
function MyClass:get_value()
return self.value
end
local obj = MyClass(3)
print(obj:get_value())
obj:set_value(4)
print(obj:get_value())
-- Inheritance
print("Inheritance")
local BaseClass = {}
BaseClass.__index = BaseClass
setmetatable(BaseClass, {
__call = function(cls, ...)
local obj = setmetatable({}, cls)
obj:_init(...)
return obj
end,
})
function BaseClass:_init(value)
-- print(debug.traceback("BaseClass::_init"))
self.value = value
end
function BaseClass:set_value(newval)
self.value = newval
end
function BaseClass:get_value()
return self.value
end
local DerivedClass = {}
DerivedClass.__index = DerivedClass
setmetatable(DerivedClass, {
__index = BaseClass, -- inheritance
__call = function(cls, ...)
local obj = setmetatable({}, cls)
obj:_init(...)
return obj
end,
})
function DerivedClass:_init(val1, val2)
BaseClass._init(self, val1)
self.value2 = val2
end
function DerivedClass:get_value()
return self.value + self.value2
end
local obj = DerivedClass(1, 2)
print(obj:get_value())
obj:set_value(3)
print(obj:get_value())
-------------------------------------------
--- Class
print('\nDog class')
Dog = {}
Dog.__index = Dog
function Dog:new()
print(debug.traceback('Dog:new()'))
obj = {sound='woof'}
return setmetatable(obj, self)
end
function Dog:makeSound()
print('I say '..self.sound)
end
-------------------------------------------
-- create object
mrDog = Dog:new()
mrDog:makeSound()
--
LoudDog = Dog:new()
LoudDog.__index = LoudDog
function LoudDog:new()
obj = {}
return setmetatable(obj, self)
end
function LoudDog:makeSound()
s = self.sound .. ' '
print(s .. s .. s)
end
seymour = LoudDog:new()
seymour:makeSound()
-- Rectangle
Rectangle = {area=0, length=0, width=0}
Rectangle.__index = Rectangle
function Rectangle:new(o, length, width)
o = o or {}
setmetatable(o, self)
o.length = length or 0
o.width = width or 0
o.area = o.length * o.width
return o
end
function Rectangle:printArea()
print("Area:", self.area)
end
function Rectangle:__tostring()
return string.format("length: %d, width: %d, area: %d", self.length, self.width, self.area)
end
r1 = Rectangle:new(nil, 10, 20)
r1:printArea()
r2 = Rectangle:new(nil, 3, 4)
r2:printArea()
r1:printArea()
print(r1)
print(r2)
--- Shape Base class
Shape = {area=0}
Shape.__index = Shape
function Shape:new(o, side)
o = o or {}
setmetatable(o, self)
o.side = side or 0
o.area = o.side * o.side
return o
end
function Shape:printArea()
print("Area: ", self.area)
end
-- Square derived class
Square = Shape:new()
Square.__index = Square
function Square:new(o, side)
o = o or Shape:new(o, side)
setmetatable(o, self)
return o
end
function Square:printArea()
print("Area of Square: ", self.area)
end
-- create object
myshape = Shape:new(nil, 10)
myshape2 = Shape:new(nil, 3)
mysquare = Square:new(nil, 20)
mysquare2 = Square:new(nil, 2)
myshape:printArea()
mysquare:printArea()
myshape2:printArea()
mysquare2:printArea()