如何循环遍历 lua 类的对象

local card = {x, y, w, h};

    function card:new(o, x, y, w, h)
        local o = o or {};
        setmetatable(o, self);
        self.__index = self;
        self.x = x;
        self.y = y;
        self.w = w;
        self.h = h;
        return o;
    end

我该如何循环遍历由此类创建的对象?

点赞
用户1442917
用户1442917

将下面翻译成中文并且保留原本的 markdown 格式,

There is no way to do this, unless you add some structure to explicitly track created objects in a table somewhere (probably with "weak" keys to allow them to be garbage collected).

It _may_ be possible to traverse all local and global values to find tables that have `__index` value pointing to that `card` table, but it's unlikely to be practical.

除非在某个表中显式跟踪创建的对象(可能使用“弱”键以允许对其进行垃圾回收),否则不能做到这一点。

可能可以遍历所有局部和全局值,以查找具有“__index”值指向该“card”表的表,但这不太可行。

2018-03-18 21:33:01