zoukankan      html  css  js  c++  java
  • quick: iskindof使用注意

    quick: iskindof使用注意

    --[[--
    
    如果对象是指定类或其子类的实例,返回 true,否则返回 false
    
    ~~~ lua
    
    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
        		-- ** 下面这行是我仿照cocos2d-x-3.11.1新加的,用于判断C++类的继承关系,如cc.Node
            if tolua.iskindof(obj, classname) then return true end
            
            mt = tolua.getpeer(obj)
        end
    
        while mt do
            if mt.__cname == classname then
                return true
            end
            mt = mt.super
        end
    
        return false
    end
     printf("UIListView ==== " .. tostring(iskindof(self.lvGrid, "UIListView")))
        printf("UIScrollView ==== " .. tostring(iskindof(self.lvGrid, "UIScrollView")))
        printf("cc.Node ==== " .. tostring(iskindof(self.lvGrid, "cc.Node")))
        printf("cc.ClippingRegionNode ==== " .. tostring(iskindof(self.lvGrid, "cc.ClippingRegionNode")))
        printf("cc.ClippingRectangleNode ==== " .. tostring(iskindof(self.lvGrid, "cc.ClippingRectangleNode")))
        printf("__cname ==== " .. tostring(self.lvGrid.__cname))
        
       
    输出结果:    
    [LUA-print] UIListView ==== true
    [LUA-print] UIScrollView ==== true
    [LUA-print] cc.Node ==== true
    [LUA-print] cc.ClippingRegionNode ==== false
    [LUA-print] cc.ClippingRectangleNode ==== true
    [LUA-print] __cname ==== UIListView
    

    注意:

    但是因为quick不能判断继承自C++如Node的情况,我实验了cocos2d-x-3.11.1中的class和iskindof方法,发现方法有很大的不同,但是printf("UIListView ==== " .. tostring(iskindof(self.lvGrid, "UIListView")))这种都判断为false的结果,网上有一篇博客修改了,http://m.blog.csdn.net/article/details?id=49799637但是我测试了下还是有问题,不过没有详细测试,最终决定还是使用quick中的class 和 iskindof方法

  • 相关阅读:
    C# 文件类的操作---删除
    C#实现Zip压缩解压实例
    UVALIVE 2431 Binary Stirling Numbers
    UVA 10570 meeting with aliens
    UVA 306 Cipher
    UVA 10994 Simple Addition
    UVA 696 How Many Knights
    UVA 10205 Stack 'em Up
    UVA 11125 Arrange Some Marbles
    UVA 10912 Simple Minded Hashing
  • 原文地址:https://www.cnblogs.com/ZhYQ-Note/p/6003080.html
Copyright © 2011-2022 走看看