Lua继承

发布于 2024-11-27 13:43:08 字数 954 浏览 1 评论 0原文

我有两个 Lua 课程。

test1 = {test1Data = 123, id= {0,3}}
function test1:hello()
    print 'HELLO!'
end
function test1:new (inp)
    inp = inp or {}
    setmetatable(inp, self)
    self.__index = self
    return inp
end

test2 = {}
function test2:bye ()
    print 'BYE!'
end
function test2:create_inst( baseClass )
    local new_class = {}
    local class_mt = { __index = new_class }
    function new_class:create()
        local newinst = {}
        setmetatable( newinst, class_mt )
        return newinst
    end
    if baseClass then
        setmetatable( new_class, { __index = baseClass } )
    end

    return new_class
end

a = test1:new({passData='abc'})
print (a.test1Data, a.passData, a:hello())
c = test2:create_inst(a)
print (c.test1Data, c.passData,c:hello(), c:bye())

我想从 test 继承 test2 但保留指定的 test2 方法bye。 除了 bye:method 之外,一切正常。 我该如何解决这个问题?

I have two classes in Lua.

test1 = {test1Data = 123, id= {0,3}}
function test1:hello()
    print 'HELLO!'
end
function test1:new (inp)
    inp = inp or {}
    setmetatable(inp, self)
    self.__index = self
    return inp
end

test2 = {}
function test2:bye ()
    print 'BYE!'
end
function test2:create_inst( baseClass )
    local new_class = {}
    local class_mt = { __index = new_class }
    function new_class:create()
        local newinst = {}
        setmetatable( newinst, class_mt )
        return newinst
    end
    if baseClass then
        setmetatable( new_class, { __index = baseClass } )
    end

    return new_class
end

a = test1:new({passData='abc'})
print (a.test1Data, a.passData, a:hello())
c = test2:create_inst(a)
print (c.test1Data, c.passData,c:hello(), c:bye())

I want to inherit test2 from test but leave in the specified test2 methods bye.
Everything works except for bye:method.
How can I solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玩套路吗 2024-12-04 13:43:08

您在 test2:create_inst() 中返回一个空表,在任何时候都不会引用 test2,因此函数 test2:bye() 是不在 test2:create_inst() 返回的表中

You return an empty table in test2:create_inst(), at no point does anything reference test2, so the function test2:bye() is not in the table returned by test2:create_inst()

救赎№ 2024-12-04 13:43:08

在您的代码中,test2 实际上与您正在实例化的表无关,您从 test2:create_inst 返回的这个 new_class 表是新表实例。所以自然地它没有名为 bye 的字段。简单修改:

function test2:create_inst( baseClass )
    ...
    if baseClass then
        setmetatable( new_class, { __index = baseClass } )
    end
    ...

    function new_class:bye()
        print("bye!")
    end
    return new_class
end

In your code, test2 actually has nothing to do with the table you are instancing, this new_class table you return from test2:create_inst is the new instance. So naturally it has no field named bye. Simple modification:

function test2:create_inst( baseClass )
    ...
    if baseClass then
        setmetatable( new_class, { __index = baseClass } )
    end
    ...

    function new_class:bye()
        print("bye!")
    end
    return new_class
end
善良天后 2024-12-04 13:43:08

我认为答案是要实现多重继承,即new_class继承“test2”和“baseClass”。

local function search(k, objlist)
    local i = 0
    local v
    while objlist[i] then
        v = objlist[i][k] 
        if v then return v end
    end
end

function class(...)
    local parents = (...)
    local object = {}
    function object:new(o)
        o = o or {}
        setmetatable(o, object)
        return o
    end
    object.__index = object
    setmetatable(object, 
        {__index=function(t, k)
        search(k, parents)    
    end})
    return object
end

c = class(a, test2)
print(c.test1Data, c.passData,c:hello(), c:bye())

I think the answer want to implement Multi-Inheritance that new_class inherit "test2" and "baseClass".

local function search(k, objlist)
    local i = 0
    local v
    while objlist[i] then
        v = objlist[i][k] 
        if v then return v end
    end
end

function class(...)
    local parents = (...)
    local object = {}
    function object:new(o)
        o = o or {}
        setmetatable(o, object)
        return o
    end
    object.__index = object
    setmetatable(object, 
        {__index=function(t, k)
        search(k, parents)    
    end})
    return object
end

c = class(a, test2)
print(c.test1Data, c.passData,c:hello(), c:bye())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文