lua-面向对象(创建与实例化)
Test1.lua
Person = {name='hzq'}
function Person:who()
print(self.name)
end
function Person:Class(name)
person = {}
setmetatable(person,{__index = self})
person.name = name
return person
end
person1 = Person:Class('yzj')
person2 = Person:Class('hhs')
person3 = Person:Class()
Test2.lua
require "Test1" person1 = Person:Class('yzj') person2 = Person:Class('hhs') person3 = Person:Class() person1:who() --输出:yzj person2:who() --输出:hhs person3:who() --输出:hzq Person:who() --输出:hzq
代码解读:

浙公网安备 33010602011771号