--[[ 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())
热门文章更多>>
标签更多>>
专题更多>>
最新文章更多>>
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
- 使用 C# 6.0 功能运行 TFS 构建