【问题标题】:lua - Access table item from within the tablelua - 从表中访问表项
【发布时间】:2015-04-09 12:49:43
【问题描述】:

首先,我刚刚启动了 Lua,所以如果这不可行或很明显,对不起。

我正在尝试做一个面向对象的实现,例如:

Parent = {
  ChildVariable = "Hello",
  ChildFunction = function ()
     print(Parent.ChildVariable)
  end  
}

我想知道的是,除了执行 'Parent.ChildVariable' 之外,我是否可以执行 'ChildVariable',它在表格中,所以我认为可以通过某种方式访问​​它。

【问题讨论】:

    标签: lua


    【解决方案1】:
    Parent = {
      ChildVariable = "Hello",
      ChildFunction = function(self)
         print(self.ChildVariable)
      end  
    }
    
    Parent:ChildFunction()
    

    【讨论】:

    • 谢谢,我读过这个但不明白,但这个例子有帮助!
    【解决方案2】:

    Lua 有一个特殊的结构:冒号操作符。 以下两行是等价的:

    tbl.func(tbl)
    

    tbl:func()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-11
      • 2014-11-10
      • 2014-06-09
      • 2018-04-12
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      相关资源
      最近更新 更多