-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.lua
327 lines (258 loc) · 6.07 KB
/
a.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
--[[print(type("Hello World"));
print(type(10.4*3));
print(type(print));
print(type(true));
print(type(nil));
print(type(type("a")));
--]]
---比尔呢修稿了
-- print(type(a));
-- a=10;
-- print(type(a));
-- a="a string!!"
-- print(type(a));
------这个地方要修改
-- a=print; --a为一个函数
-- a("name")
-- a="one string" --替换
-- b=string.gsub(a,"one","another")
-- print(a,b)
-- print("10"+1)
-- print("10+1")
-- print("10"+"1")
-- -- print("hello"+1)
-- print("Hello".."World")--..链接字符串
-- print("10"..20)
-- print(10 ..20)
--天下之大何其不有
-- a=20
-- print(tonumber(a))--装换为number不成功为nil
-- m="aa"
-- if tostring(m) then
-- print("Success")
-- else
-- print("fail")
-- local a = 19 --局部变量 在外面是不能打印的
-- end
-- print(a)
--lua 表达式
-- print(4 and false) --如果a为false返回a 否则返回b
-- print(nil and 13)
-- print(false and 13)
-- print(4 or 5)
-- print(false or 5)
-- print((4 and false) or 4)
-- a,b,c,d=20,30,50 --此时abcd都是全局变量
-- print(a,b,c,d,e)
-- a,b=b,a --交换数据
-- print(a,b)
-- for i=1,100 ,10 do --从1到100 循环 10表示梯度i+10
-- print(i)
-- end
-- local m = 1
-- while true do
-- m=m+1
-- print(m)
-- if m==50 then
-- break
-- end
-- end
-- function max( a,b ) --函数
-- if a>b then
-- return a
-- else
-- return b,a
-- end
-- end
-- print(max(5,10)) --调用max函数
-- function test()
-- return 10,20,30 --lua 可以返回多个值
-- end
-- a,b,c=test(),50 --如果调用函数在中间或者前面都只能返回一个值,放在末尾可以返回多个值(补齐作用)
-- print(a,b,c)
-- function a( ... )
-- function b( ... )
-- print("B called")
-- end
-- b()
-- end
-- a() --在函数外面如果函数嵌套函数 要先调用外面的函数才能调用里面的函数
-- b()
--days={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}
-- print(days[5]) --lua数组是从1开始的
--config={name="zhangsan",age=20,"Wednesday","Saturday","Friday"} --建值对
-- print(config.name)[2]=
-- print(config.name)
-- print(config["name"])
-- config.sex="boy" --添加一个键值对
-- config["sex"]="boy"
-- for k,v in pairs(config) do --ipairs是连续(按下标)的遍历数组中的元素不能遍历字典
-- print(k,v) --pairs是遍历字典和数组所有的内容
-- end
-- arr={}
-- for var=1,100 do
-- table.insert(arr,1,var) --每次从1这个位置插入数据
-- end
-- print(#arr) --打印表的长度
-- print(table.maxn(arr)) --打印最大值
-- for i=1,100 do
-- print(arr[i])
-- end
-- function count()--闭合函数
-- local i = 1
-- return function ()
-- i=i+1;
-- return i;
-- end
-- end
-- local fun = count();
-- print(fun())
-- print(fun())
-- print(fun())
-- i=10;j="10";k="+10"
-- a={}
-- a[i]="one value"
-- a[j]="another value"
-- a[k]="yet another value"
-- print(a[j])
-- print(a[k])
-- print(a[i])
-- print(a[tonumber(i)])
-- print(a[tonumber(j)])
-- print(a[tonumber(k)])
-- a={1,2,3,1000}
-- --a[10000]=1
-- print(table.maxn(a))--最大的索引
-- local eat; --
-- local drink;--有点像前向声名
-- eat =function()
-- print("eat")
-- return drink();
-- end
-- drink=function()
-- print("drink")
-- end
-- print(eat())
-- local function test()
-- local test
-- test=function()
-- local drink --尾掉式
-- local function eat()
-- print("eat");
-- return drink()+1
-- end
-- drink=function()
-- return 10;
-- end
-- for i=1,100000 do
-- eat()
-- end
-- function dieDaiQi(t)
-- local i=0
-- return function()
-- i=i+1
-- return t[i]
-- end
-- end
-- local t={"a","b","c","d"}
-- local iter=dieDaiQi(t)
-- while true do
-- local value=iter()
-- if value==nil then
-- break;
-- end
-- print(value)
-- end
-- end
-- end
-- return i,t[i];
-- return nil;
-- end
-- i=i+1
-- if i>#t then
-- local i=0
-- return function()
-- function dieDaiQi(t)
-- local t={"fdsd","445"}
-- for value,v in dieDaiQi(t) do
-- print(value,v);
-- end;
-- local name = "zhangsan1" --出错i
-- -- error("出错了")
-- if name~="zhangsan" then
-- error("I want to find zhangsan")
-- end
-- local name = "张三"
-- local result = assert(name=="张三","你不是张三,我要找的人是张三") --成功返回ture
-- print(result);
function test( )
print(a[1])
end
local statues,err= pcall(test)
if statues then
print("正常,呵呵")
else --捕获错误代码——pcall
print("函数出错了,我正在帮你处理")
print(err)
end
-- function test( )
-- print(a[1])
-- end
-- if pcall(test) then
-- print("正常")
-- else
-- print("函数出错了,我正在帮你处理")
-- print(error())
-- end
--table的深拷贝
--如何计算一个table的长度
-- w={x=0,y=0,label="console"}
-- x={math.sin(0),math.sin(1),math.sin(2)}
-- w[1]="another field" --向表里面添加数组
-- x.f=w
-- print(w.x)
-- print(w[1])
-- print(x.f[1])
-- for k,v in ipairs(w) do
-- print(k,v)
-- end
-- --w.x=nil
-- polyLine={color="blue",thickness=2,npoints=4,
-- {x=0,y=0},
-- {x=-10,y=0},
-- {x=-10,y=1},
-- {x=0,y=1}
-- }
-- print(polyLine[2].x)
-- print(polyLine[4].y)????
-- for i=10 ,1,-1 do
-- print(1)
-- end
-- local mt= {}
-- mt.__add=function(s1,s2)--元方法
-- local result = ""
-- if s1.sex=="boy" and s2.sex=="girl" then
-- result="完美的家庭"
-- elseif s1.sex=="girl" and s2.sex=="girl" then
-- result="不好"
-- else
-- result="不好"
-- end
-- return result
-- end
-- local s1 = { name="Hello",sex="boy"}
-- local s2 = {name="good",sex="girl"}
-- setmetatable(s1, mt)
-- setmetatable(s2, mt)
-- local result = s1+s2
-- print(result)
local t = { name="hehe"}
print(t.money)
local t = { name="hehe"}
local mt = {
__index=function(table,key)
print("虽然你调用了不存在的字段,不过没关系" .. key);
end
}
setmetatable(t, mt);
print(t.money)