【问题标题】:Lua class not workingLua类不工作
【发布时间】:2013-08-23 14:25:35
【问题描述】:

我在 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' 附近

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

【问题讨论】:

    标签: lua lua-table metatable


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 2011-09-01
      • 2014-12-21
      • 2018-12-28
      • 1970-01-01
      • 2014-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多