国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Lua編程示例(二):面向對象、metatable對表進行擴展

2024-07-21 23:04:59
字體:
來源:轉載
供稿:網友
counter = { count = 0}function counter.get(self) return self.countendfunction counter:inc() self.count=self.count+1endprint(counter.get(counter))counter.inc(counter)print(counter.get(counter))counter2={ count=4, get = counter.get, inc = counter.inc,}print(counter2:get())counter.inc(counter2)print(counter2.get(counter2))print()tb1 ={ "alpha","beta","gamma"}mt={}setmetatable(tb1,mt)print(getmetatable(tb1) == mt)print()function mt.__add(a,b) local result = setmetatable({},mt) for i=1,#a do table.insert(result,a[i]) end for i=1,#b do table.insert(result,b[i]) end return resultendtb2={"aaa","bbb","ccc"}res=tb1+tb2for i,v in ipairs(res) do print(v)endprint()function mt.__unm(a) local result = setmetatable({},mt) for i=#a , 1 ,-1 do table.insert(result,a[i]) end return resultendres=-tb1+tb2for i,v in ipairs(res) do print(v)endprint()function mt.__tostring(a) local result = "{" for i,v in ipairs(a) do result = result.." "..v end result = result.." } " return resultendprint(tb1)function mt.__index(tb1,key) print("there is no "..key.." in the table") return nilendprint(tb1["fsy"])function mt.__newindex(a,key,v) if( key == "haha") then error(" Stop laugh!",2) else rawset(a,key,v) endendtb1.haha="heihei"

 
運行結果:

0145truealphabetagammaaaabbbcccgammabetaalphaaaabbbccc{ alpha beta gamma } there is no fsy in the tablenillua: my_test.lua:166: Stop laugh!stack traceback: [C]: in function 'error' my_test.lua:160: in function <my_test.lua:158> my_test.lua:166: in main chunk [C]: ?

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沙湾县| 海原县| 枞阳县| 交城县| 汉寿县| 林西县| 乌兰浩特市| 郧西县| 巴青县| 朔州市| 郑州市| 灵川县| 大城县| 全南县| 肥东县| 宿迁市| 西乌珠穆沁旗| 阿城市| 九龙坡区| 呼图壁县| 绥江县| 石台县| 永平县| 邓州市| 宝山区| 乐至县| 柘城县| 长葛市| 聂荣县| 大足县| 濮阳县| 农安县| 龙游县| 瑞昌市| 佳木斯市| 泰顺县| 凤台县| 宝丰县| 小金县| 临高县| 嵩明县|