阅读量:0
Lua 类的继承
基类
-- 基类 BaseObject = {} function BaseObject:new(o) --construct o = o or {} setmetatable(o, self) self.__index = self return o end function BaseObject:myFunc() print("call BaseObject:myFunc") end function BaseObject:myFunc2() print("call BaseObject:myFunc") end return BaseObject
派生类
local BaseObject = require('BaseObject') -- 派生类 MyObject = BaseObject:new() function MyObject:new(o) -- construct o = o or {} setmetatable(o, self) self.__index = self return o end function MyObject:myFunc2() print("call MyObject:myFunc2") end return MyObject
使用
local MyObject = require('MyObject') -- 创建对象 local obj = MyObject:new() obj:myFunc() obj:myFunc2()