Class Lua类:运行其中一个对象时如何访问该对象';什么方法?
我用这个来上课: 这是我的密码:Class Lua类:运行其中一个对象时如何访问该对象';什么方法?,class,oop,lua,coronasdk,Class,Oop,Lua,Coronasdk,我用这个来上课: 这是我的密码: require 'class' -- General node class and functions Node = class(function(node, ID, x, y) node.nodetype = 'neutral'; node.id = ID; node.x = x; node.y = y; node.fname = 'exnode.png' end) function Node:display() pr
require 'class'
-- General node class and functions
Node = class(function(node, ID, x, y)
node.nodetype = 'neutral';
node.id = ID;
node.x = x;
node.y = y;
node.fname = 'exnode.png'
end)
function Node:display()
print( self.fname )
local img = display.newImage( self.fname, self.x, self.y );
img:addEventListener( "tap", self.onTap )
end
运行初始化实例的显示功能时。我收到一个错误,说明未定义
self
。如何获取对象的属性?正如Etan在评论中所说,确保将方法调用为object:method()
,而不是object。method()
,因为在第一种情况下self
值设置为object
,而在第二种情况下,它没有设置为任何值(因此有nil
值)object:method(…)
notation是object.method(object,…)
调用函数时是否使用obj.display()
或obj:display()
?前者不会将obj
传递给函数(如self
),后者会。@EtanReisner ohhhh我不知道,但在我尝试obj:display()后,我得到了相同的错误。如果您修复了它,请发布一个描述如何接受它的答案,以便其他人可以从您的答案中学习。