1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| local function clone(object) local lookup_table = {} local function _copy(object) if type(object) ~= "table" then return object elseif lookup_table[object] then return lookup_table[object] end local new_table = {} lookup_table[object] = new_table for key, value in pairs(object) do new_table[_copy(key)] = _copy(value) end return setmetatable(new_table, getmetatable(object)) end return _copy(object) end
local function class(super) local cls
if (super) then cls = clone(super) else cls = { ctor = function() end } end
function cls.new(...) local sets, gets = {}, {} local instance = setmetatable({ }, { __index = function(t, k) if gets[k] then return gets[k]() end return cls[k] end, __newindex = function(t, k, v) if sets[k] then return sets[k](v) end rawset(t, k, v) end, }) if instance.setter or instance.getter then error("error : Prohibition of override 'setter' and 'getter' Function;"); end instance.setter = function(_, name, func) instance[name] = nil sets[name] = func return instance end instance.getter = function(_, name, func) gets[name] = func return instance end
instance.class = cls instance:ctor(...) return instance end
return cls end
|