3

我在 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: '=' 预期在 'name' 附近

我不确定发生了什么或为什么会发生这种情况,任何帮助将不胜感激。

4

1 回答 1

5

更改print nameprint(name)print只是一个常规函数,并且函数调用需要括号,除非它们使用单​​个参数调用,该参数可以是字符串文字或表文字。

于 2013-08-22T23:55:21.080 回答