Lua自己实现如果对象是指定类或其子类的实例,返回 true

来源:互联网 发布:复制淘宝口令没反应 编辑:程序博客网 时间:2024/05/16 23:46
如果对象是指定类或其子类的实例,返回 true,否则返回 false

local Animal = class("Animal")
local Duck = class("Duck", Animal)
print(iskindof(Duck.new(), "Animal")) -- 输出 true


@param mixed obj 要检查的对象
@param string classname 类名
@return boolean

function iskindof(obj, classname)    local t = type(obj)    local mt    if t == "table" then        mt = getmetatable(obj)    elseif t == "userdata" then        mt = tolua.getpeer(obj)    end    while mt do        if mt.__cname == classname then            return true        end        mt = mt.super    end    return falseend

0 0
原创粉丝点击