Lua程序设计(二)面向对象概念介绍
原创
©著作权归作者所有:来自51CTO博客作者Tinywan2026的原创作品,请联系作者获取转载授权,否则将追究法律责任


----------------------------------------------------------- Lua面向对象3
local smartMan = {
name = "Tinywan",
age = 26,
money = 800000,
sayHello = function()
print("Tinywan say 大家好")
end
}
local t1 = {}
local mt = {
__index = smartMan,
__newindex = function(table, key, value)
print(key .. "字段不存在不要试图给他赋值")
end
}
setmetatable(t1, mt)
t1.sayHello = function()
print("HAHA")
end
t1.sayHello()
--- 输出结果
-- sayHello字段不存在不要试图给他赋值
-- Tinywan say 大家好


----------------------------------------------------------- Lua面向对象3
local smartMan = {
name = "none"
}
local other = {
name = "大家好,我是无赖的table"
}
local t1 = {}
local mt = {
__index = smartMan,
__newindex = other
}
setmetatable(t1, mt)
print("other的名字,赋值前:" .. )
= "峨眉大侠"
print("other的名字,赋值后:" .. )
print("t1 的名字:" .. )
--- 输出结果
-- other的名字,赋值前:大家好,我是无赖的table
-- other的名字,赋值后:峨眉大侠
-- t1 的名字:none










有问题




local 变量不放在全局函数中去

以上不需要 return 返回
