Lua里模拟typeof()获取对象类型
自定义typeof()函数,获取"对象"类型
function typeof(var) local _type = type(var); if(_type ~= "table" and _type ~= "userdata") then print('---1')return _type; end local _meta = getmetatable(var); if(_meta ~= nil and _meta._NAME ~= nil) then print('---2') return _meta._NAME; else print('---3') return _type; endendXC={}--基类function XC:new(o)o = o or {}setmetatable(o, self)self.__index = selfreturn oendfunction XC:extend() o = {} setmetatable(o, self) self.__index = self return oendXBG = XC:extend() --派生类XBG._NAME='XBG'b1=XBG:new() --创建对象print(typeof(b1))输出:XBG