father = {house = 1}
son = {car = 2}
function father:printHouse()
print("father house is ",self.house)
end
function father:printHello()
print("father hello")
end
function son:printHouse()
print("son house is ",self.house)
end
function test()
father:printHouse()
son:printHouse()
setmetatable(son,father)
father.__index = father
son:printHouse()
father.__newindex = father
son.house = 2
son:printHouse()
father:printHouse()
son:printHello()
father:printHello()
end输出:
[LUA-print] son house is nil
[LUA-print] son house is 1
[LUA-print] son house is 2
[LUA-print] father house is 2
[LUA-print] father hello
[LUA-print] father hello
4235




被折叠的 条评论
为什么被折叠?



