您的位置:首页 > 编程语言 > Lua

在Lua Development Tools中使用代码帮助

2014-02-25 15:59 471 查看
事实上1.2M1之前的版本对于继承支持的不是很好,今天试用了1.2M1发现,已经支持的很好了

咱们先看一下示例代码,这样就明白怎么来继承了

local function Class(super)
local Cls = {}
Cls.__index = Cls
local mtable = {
__call = function(cls, ...)
return cls.new(...)
end
}

if super ~= nil then
mtable.__index = super
end

setmetatable(Cls, mtable)

function Cls.new()
return setmetatable({}, Cls)
end

return Cls
end

---父类定义
--@type Parent
local Parent = Class()

---父类构造函数
--@function create
--@return #Parent
function Parent.create()
return Parent.new()
end

---设置名称
--@function [parent=#Parent] setName
--@param self
--@param name #string
function Parent:setName(name)
self.name = name
end

---打印信息
--@function [parent=#Parent] print
--@param self
function Parent:print()
print("Parent " .. self.name)
end

---子类定义
--@type Child
--@extends #Parent
local Child = Class(Parent)

---子类构造函数
--@function [parent=#Child] create
--@return #Child
function Child.create()
return Child()
end

---子类重载的打印信息函数
--@function [parent=#Child] print
--@param self
function Child:print()
print("Child " .. self.name)
end

local parent = Parent.create()
parent:setName("hello")
parent.y = 'hello'
parent:print()

local child = Child.create()
child:setName("hello, world")
child:print()


从上面的示例可以看出来,1.2M1里多了一个叫extends的关键字,这个关键字来告诉ldt继承关系
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: