Lua 类的继承

avatar
作者
猴君
阅读量: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() 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!