-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.lua
More file actions
141 lines (119 loc) · 3.56 KB
/
Copy pathdemo.lua
File metadata and controls
141 lines (119 loc) · 3.56 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
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
require "simpleclass"
--#==========================================
--# 基础使用
--#==========================================
class "Person" {
---@param name string
---@param age integer
__init = function(self, name, age)
self.name = name
self.age = age
end;
sayHello = function(self)
print("Hello, my name is ".. self.name.. " and I am ".. self.age.. " years old.")
end;
}
class "Student" : extends "Person" {
---@param name string
---@param age integer
---@param grade string
__init = function(self, name, age, grade)
super(Student, self):__init(name, age)
self.grade = grade
end;
---@override
sayHello = function(self)
print("Hello, my name is ".. self.name.. " and I am a ".. self.grade.. " year old student.")
end;
}
local p1 = Person("John", 25)
p1:sayHello()
-- Output: Hello, my name is John and I am 25 years old.
local s1 = Student:new("Jane", 20, "senior")
s1:sayHello()
-- Output: Hello, my name is Jane and I am a senior year old student.
print(s1:isInstance(Student)) --> true
print(s1:isInstance(Person)) --> true
print(s1:isInstance(object)) --> true
print(isinstance(s1, "table")) --> true
print(cls_type(s1)) --> Student
--#==========================================
--# 深层继承
--#==========================================
class "CollageStudent" : extends "Student" {
---@param name string
---@param age integer
---@param grade string
__init = function(self, name, age, grade)
super(CollageStudent, self):__init(name, age, grade)
end;
}
local c1 = CollageStudent("Alice", 22, "junior")
c1:sayHello()
-- Output: Hello, my name is Alice and I am a junior year old student.
--#==========================================
--# 接口使用
--#==========================================
interface "CanEat" {"eat"}
interface "CanFly" {"fly"}
-- 错误的定义
xpcall(function()
class "Bird_wrong" : implements(CanEat, CanFly) {
eat = function()
print("Bird eats bugs")
end;
}
end, print)
-- Output: class Bird_wrong implements <interface 'CanFly'> but dose not implement method 'fly'.
-- 正确的定义
class "Bird" : implements(CanEat, CanFly) {
eat = function(self)
print(self:getClass():toString().." eats bugs")
end;
fly = function(self)
print(self:getClass():toString().." is flying")
end;
}
local bird = Bird()
bird:eat() --> Bird eats bugs
bird:fly() --> Bird is flying
-- 接口组合
interface "BirdLike" : extends(CanEat, CanFly) {
"spawn";
"nest";
}
-- 同时进行继承与实现接口
class "Eagle" : extends "Bird" : implements(BirdLike) {
spawn = function(self)
print(self:getClass():toString().." is spawning")
end;
nest = function(self)
print(self:getClass():toString().." is nesting")
end;
}
local eagle = Eagle()
eagle:eat() --> Eagle eats bugs
eagle:fly() --> Eagle is flying
eagle:spawn() --> Eagle is spawning
eagle:nest() --> Eagle is nesting
print(Eagle:isImplements(BirdLike)) --> true
print(eagle:isInstance(BirdLike)) --> true
--#==========================================
--# Getter / Setter
--#==========================================
class "Account" {
---@param balance? number
__init = function(self, balance)
self._balance = balance or 0
end;
['get.balance'] = function(self)
return self._balance
end;
['set.balance'] = function(self, value)
self._balance = value
end;
}
local account = Account(100)
print(account.balance) --> 100
account.balance = 200
print(account.balance) --> 200