源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Lua模拟面向对象示例分享

  • 时间:2020-11-27 23:18 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Lua模拟面向对象示例分享
代码很简单,这里就不多废话了,大家主要看看思路
[u]复制代码[/u] 代码如下:
function class(super)     local mt = {__call = function(_c, ...)         local function create(_c, _o, ...)             if _c.__super then create(_c.__super, _o, ...) end             if _c.__ctor then _c.__ctor(_o, ...) end             return _o         end         local _o = create(_c, {}, ...)         return setmetatable(_o, _c)     end}     mt.__index = super or mt     return setmetatable({__super = super}, mt) end ---------------------------------------------------------------------- A = class() function A:__ctor(s)     self.i = 123     self.j = 333     print('A ctor', s) end local a = A('a') print(a.i, a.j) -- B继承A B = class(A) function B:__ctor(s)     self.i = 444     print('B ctor', s) end local b = B('b') print(b.i, b.j)
示例截图 [img]http://files.jb51.net/file_images/article/201503/2015030916401411.jpg[/img] 以上就是本文的全部内容了,希望大家能够喜欢。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部