-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.lua
More file actions
105 lines (105 loc) · 3.53 KB
/
class.lua
File metadata and controls
105 lines (105 loc) · 3.53 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
97
98
99
100
101
102
103
104
105
local Point = class(function(Point)
local function __init__(kvs, self, x, y)
self = get_posarg(kvs, 'self', self, nil, '__init__')
x = get_posarg(kvs, 'x', x, nil, '__init__')
y = get_posarg(kvs, 'y', y, nil, '__init__')
self.x = x
self.y = y
end
local function get_x(kvs, self)
self = get_posarg(kvs, 'self', self, nil, 'get_x')
return self.x
end
local function get_y(kvs, self)
self = get_posarg(kvs, 'self', self, nil, 'get_y')
return self.y
end
Point.__init__ = __init__
Point.get_x = get_x
Point.get_y = get_y
return Point
end, {}, "Point")
local p = Point(merge_kwargs({}, {}), 5, 10)
assert(bool({}, (p.get_x(merge_kwargs({}, {})) == 5)))
assert(bool({}, (p.get_y(merge_kwargs({}, {})) == 10)))
assert(bool({}, (p.x == 5)))
assert(bool({}, (p.y == 10)))
local Dog = class(function(Dog)
local species = "mammal"
local function __init__(kvs, self, name, age)
self = get_posarg(kvs, 'self', self, nil, '__init__')
name = get_posarg(kvs, 'name', name, nil, '__init__')
age = get_posarg(kvs, 'age', age, nil, '__init__')
self.name = name
self.age = age
end
Dog.species = species
Dog.__init__ = __init__
return Dog
end, {}, "Dog")
local d = Dog(merge_kwargs({}, {}), "roster", 18)
assert(bool({}, (Dog.species == "mammal")))
assert(bool({}, (d.name == "roster")))
assert(bool({}, (d.age == 18)))
local Person = class(function(Person)
local function __init__(kvs, self, name, age)
self = get_posarg(kvs, 'self', self, nil, '__init__')
name = get_posarg(kvs, 'name', name, nil, '__init__')
age = get_posarg(kvs, 'age', age, nil, '__init__')
self.name = name
self.age = age
end
local function talk(kvs, self)
self = get_posarg(kvs, 'self', self, nil, 'talk')
return "person talks"
end
Person.__init__ = __init__
Person.talk = talk
return Person
end, {}, "Person")
local Chinese = class(function(Chinese)
local function __init__(kvs, self, name, age, country)
self = get_posarg(kvs, 'self', self, nil, '__init__')
name = get_posarg(kvs, 'name', name, nil, '__init__')
age = get_posarg(kvs, 'age', age, nil, '__init__')
country = get_posarg(kvs, 'country', country, nil, '__init__')
Person.__init__(merge_kwargs({}, {}), self, name, age)
self.country = country
end
local function walk(kvs, self)
self = get_posarg(kvs, 'self', self, nil, 'walk')
return "chinese walks"
end
Chinese.__init__ = __init__
Chinese.walk = walk
return Chinese
end, {Person}, "Chinese")
local American = class(function(American)
local function talk(kvs, self)
self = get_posarg(kvs, 'self', self, nil, 'talk')
return "I love talk show"
end
American.talk = talk
return American
end, {Person}, "American")
local c = Chinese(merge_kwargs({}, {}), "john", 18, "China")
assert(bool({}, (c.name == "john")))
assert(bool({}, (c.age == 18)))
assert(bool({}, (c.country == "China")))
assert(bool({}, (c.talk(merge_kwargs({}, {})) == "person talks")))
assert(bool({}, (c.walk(merge_kwargs({}, {})) == "chinese walks")))
local a = American(merge_kwargs({}, {}), "jane", 24)
assert(bool({}, (a.name == "jane")))
assert(bool({}, (a.age == 24)))
assert(bool({}, (a.talk(merge_kwargs({}, {})) == "I love talk show")))
return {
Point = Point,
p = p,
Dog = Dog,
d = d,
Person = Person,
Chinese = Chinese,
American = American,
c = c,
a = a,
}