Lua 面向对象的原理
lua本身是没有类的。但lua有元表和元方法,通过元表和元方法我们可以实现类的一些特征。
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