笨木头花心贡献,哈?花心?不,是用心~
转载请注明,原文地址:http://www.benmutou.com/archives/1758
文章来源:笨木头与游戏开发
[cce] -- 创建一个元表 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 [/cce]
[cce] -- 创建两个table,可以想象成是两个类的对象 local s1 = { name = "Hello", sex = "boy", }; local s2 = { name = "Good", sex = "girl", }; -- 给两个table设置新的元表 setmetatable(s1, mt); setmetatable(s2, mt); -- 进行加法操作 local result = s1 + s2; print(result); [/cce]其实我们可以把s1和s2当成是类的对象,实际上Lua也可以模拟类的结构,目前暂时不说,配合看书的进度~