Lua类不起作用。

我在 Lua 中有一个简单的类实现。

test = {}
test.__index = test

function test:new()
    local o = {}
    setmetatable(o, self)
    return o
end

function test:setName(name)
    self.name = name
    print name
end

local name = test:new()
name:setName("hello")

当我运行它时,我始终会得到以下错误:

lua: test.lua:12: '=' expected near 'name'

我不确定为什么会出现这种情况,希望能得到帮助。

点赞
用户127833
用户127833

print name 改为 print(name)print 只是一个普通函数,函数调用需要括号,除非它们被调用时带有一个字符串字面量或表字面量作为单个参数。

2013-08-22 23:55:21