Person = {name = "张三", age = 25}
function Person:eat()
print(self.name.." is eating")
print(self.name.."'s age is "..self.age)
end
--a = Person
--a.eat(a)
function Person:new(o)
local t = o or {}
setmetatable( t, { __index=self }) --调用一个属性的时候,如果t中不存在,那么会在__index 所指定的table中查找
self.__index = self
return t
end
Person1 = Person:new()
Person1.name = "李四"
Person1.age = 30
Person1:eat()
Person1.weight = 80
print(Person1.weight)
结果:
李四 is eating
李四's age is 30
80
网友评论