Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class内的Super的使用问题 #131

Closed
unicodecndddd opened this issue Mar 19, 2020 · 1 comment
Closed

Class内的Super的使用问题 #131

unicodecndddd opened this issue Mar 19, 2020 · 1 comment

Comments

@unicodecndddd
Copy link

目前unlua内的Class定义的Super只能调用直接继承的lua内的方法。如果继承了多层,Super可能会失效,因为在没有原表的情况下,Super是不会主动向下遍历Super的。大概类似如下代码:

a.lua
local a = Class()

function a:test()
end

return a

--------------------------------------

b.lua
local b = Class("a")

--------------------------------------

c.lua
local c = Class("b")
function a:test()
    self.Super.test(self) --error, not find function test
end
@rowechien
Copy link
Collaborator

rowechien commented Mar 20, 2020

@unicodecndddd Super只是简单模拟了“继承”的语义,“父类”不是Class()返回的表的元表,只设置在Super这个field上(元表在插件的c++代码里定义了,这个过程已经固化)。单就你这个场景,只能这样写:
local c = Class("b")
function c:test()
c.Super.Super.test(self)
end
当然难看了点。。。当“继承”链上定义了多个相同函数,最好直接用表来访问Super,而不是self.Super

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants