This repository was archived by the owner on Feb 8, 2022. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.lua
More file actions
Latest commit
96 lines (78 loc) · 2.05 KB
/
Copy pathexample.lua
File metadata and controls
96 lines (78 loc) · 2.05 KB
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
localObject=require("init")
---Extra subclass. Just for test.
---@classlib.object.example-proto-point:lib.object
---@fieldsuperlib.object
localProtoPoint=Object:extend("lib.object.example-proto-point")
---@classlib.object.example-point:lib.object.example-proto-point
---@fieldsuperlib.object
localPoint=ProtoPoint:extend("lib.object.example-point")
Point.scale=2-- Class field!
functionPoint:init(x, y)
self.x=xor0
self.y=yor0
returnself
end
functionPoint:resize()
self.x=self.x*self.scale
self.y=self.y*self.scale
returnself
end
functionPoint.__call()
return"called"
end
---@classlib.object.example-rect:lib.object.example-point
---@fieldsuperlib.object.example-point
---@fieldwidthinteger
---@fieldheightinteger
localRectangle=Point:extend("lib.object.example-rect")
functionRectangle:resize()
Rectangle.super.resize(self) -- Extend Point's `resize()`.
self.w=self.w*self.scale
self.h=self.h*self.scale
returnself
end
functionRectangle:init(x, y, w, h)
Rectangle.super.init(self, x, y) -- Initialize Point first!
self.w=wor0
self.h=hor0
returnself
end
functionRectangle:__index(key)
ifkey=="width" then
returnself.w
end
ifkey=="height" then
returnself.h
end
returnrawget(self, key)
end
functionRectangle:__newindex(key, value)
ifkey=="width" then
self.w=value
elseifkey=="height" then
self.h=value
end
returnrawset(self, key, value)
end
-- local rect = Rectangle:new():init(2, 4, 6, 8)
localrect=Rectangle(2, 4, 6, 8)
-- rect:new() -- Error: rect is not a class.
rect.new="wtf"
assert(rect.new=="wtf")
functionrect:new()
returnself
end
rect:new() -- No errors. Own `new()` and `extend()` are allowed.
assert(rect.w==6)
assert(rect:is(Rectangle))
assert(rect:is("lib.object.example-rect"))
assert(notrect:is(Point))
assert(rect:has("lib.object.example-point") ==1)
assert(Rectangle:has(Object) ==3)
assert(rect() =="called")
rect.width=666
assert(rect.w==666)
assert(rect.height==8)
fork, v, tinrect:iter() do
print(k, v, t)
end