----------------------------------------------------------- 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的名字,赋值前:" .. other.name) t1.name = "峨眉大侠" print("other的名字,赋值后:" .. other.name) print("t1 的名字:" .. t1.name) --- 输出结果 -- other的名字,赋值前:大家好,我是无赖的table -- other的名字,赋值后:峨眉大侠 -- t1 的名字:none
有问题
local 变量不放在全局函数中去
以上不需要 return 返回