首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

Lua里模拟typeof()获取对象部类

2012-08-31 
Lua里模拟typeof()获取对象类型自定义typeof()函数,获取对象类型function typeof(var)local _type typ

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

热点排行