帝王谷资源网 Design By www.wdxyy.com

mulInherit.lua

复制代码 代码如下:
--[[
author:looyer@sina.com
date:2014/7/18
purpose:lua的继承演示
--]]
--- base class "Object"
local Object = {_t = "Object"}
function Object:new(id)
    local o =
    {
        _id = id
    }
    setmetatable(o, self)
    self.__index = self
    return o
end
function Object:type()
    return self._t
end
function Object:id()
    return self._id
end
--- class "Widget" driver from "Object"
local Widget = {_t = "Widget"}
setmetatable(Widget, Object)
function Widget:new(id)
    local o =
    {
        _id = id,
    }
    setmetatable(o, self)
    self.__index = self
    -- function (tb, key)
    --  return self[key]
    -- end
    return o
end
-- function Widget:id()
--  return self._id
-- end
--- class "Drawor" driver from "Object"
local Drawor = {_t = "Drawor"}
setmetatable(Drawor, Object)
function Drawor:new(id, name)
    local o =
    {
        _id = id,
        _name = name,
    }
    setmetatable(o, self)
    self.__index = self
    return o
end
function Drawor:name()
    return self._name
end
--- class "DrawPlane" driver from "Widget" and "Drawor"
local DrawPlane = {_t = "DrawPlane", _father = {Widget, Drawor}}
function DrawPlane:new(id)
    local o =
    {
        _id = id,
    }
    setmetatable(o, self)
    self.__index =
    function (tb, key)
        if self[key] then return self[key] end
        for _, v in pairs(self._father) do
            if v._t == key then return v end
            if v[key] then return v[key] end
        end
    end
    return o
end
local varA = Object:new()
local varB = Widget:new(10)
local varC = Drawor:new(30, "dwr")
local varD = DrawPlane:new(51)
print("varA type:", varA:type())
print("varB type:", varB:type())
print("varB._id: ", varB._id, varB:id())
print("varC name: ", varC:name())
print("varD type: ", varD:type())
print("varD id: ", varD:id())
print("varD father Widget--", varD.Widget:type())

以上就是本文所要分享的代码了,希望大家能够喜欢。

标签:
Lua,类继承

帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com