-- class.lua -- Compatible with Lua 5.1 (not 5.0). function Class(base, _ctor) local c = { } return xClass(c, base, _ctor); end function xClass(c, base, _ctor) -- a new class instance if not _ctor and type(base) == 'function' then _ctor = base base = nil elseif type(base) == 'table' then -- our new class is a shallow copy of the base class! for i, v in pairs(base) do c[i] = v end c._base = base end -- the class will be the metatable for all its objects, -- and they will look up their methods in it.clcl c.__index = c -- expose a constructor which can be called by () local mt = { } mt.__call = function(class_tbl, ...) local obj = { } setmetatable(obj, c) if _ctor then _ctor(obj, ...) end return obj end c._ctor = _ctor c.is_a = function(self, klass) local m = getmetatable(self) while m do if m == klass then return true end m = m._base end return false end setmetatable(c, mt) return c end function quick_class(classname, super) local superType = type(super) local cls if superType ~= "function" and superType ~= "table" then superType = nil super = nil end if superType == "function" or (super and super.__ctype == 1) then -- inherited from native C++ Object cls = {} if superType == "table" then -- copy fields from super for k,v in pairs(super) do cls[k] = v end cls.__create = super.__create cls.super = super else cls.__create = super cls.ctor = function() end end cls.__cname = classname cls.__ctype = 1 function cls.new(...) local instance = cls.__create(...) -- copy fields from class to native object for k,v in pairs(cls) do instance[k] = v end instance.class = cls instance:ctor(...) return instance end else -- inherited from Lua Object if super then cls = {} setmetatable(cls, {__index = super}) cls.super = super else cls = {ctor = function() end} end cls.__cname = classname cls.__ctype = 2 -- lua cls.__index = cls function cls.new(...) local instance = setmetatable({}, cls) instance.class = cls instance:ctor(...) return instance end --扩展create方法 function cls.create(...) return cls.new(...) end end return cls end