Lua 类的继承
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()
原文地址:https://blog.csdn.net/z2014z/article/details/140713949
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!