Lua课不起作用

cht*_*son 3 lua metatable lua-table

我在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")
Run Code Online (Sandbox Code Playgroud)

我运行时不断收到此错误:

lua:test.lua:12:'='预计在'name'附近

我不确定这是什么或为什么会发生这种情况,我们将非常感谢任何帮助.

lea*_*afo 5

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