Lua模拟面向对象示例分享
代码很简单,这里就不多废话了,大家主要看看思路
代码如下:
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)
示例截图
以上就是本文的全部内容了,希望大家能够喜欢。
相关推荐
峰哥 2020-09-23
陈云佳 2020-08-15
wqiaofujiang 2020-07-05
wordmhg 2020-06-26
wqiaofujiang 2020-06-16
zllbirdonland 2020-06-16
eroshn 2020-06-10
长安长夜Saint 2020-06-07
Dawnworld 2020-06-07
fansenjun 2020-03-01
CSDNMrWang 2020-05-11
Dawnworld 2020-05-05
陈云佳 2020-04-21
Neptune 2020-04-20
shunelly 2020-04-16
aolishuai 2020-04-15
YukiRain 2020-04-14
陈云佳 2020-03-07
陈云佳 2020-03-05