Lua 面向对象编程 - 我不明白如何新建“类”的实例?

Kel*_*lly 3 oop lua

我正在构建一个简单的游戏,并且一直在关注 PIL 书。这是我的游戏基于的场景: http ://www.lua.org/pil/16.1.html

我很挣扎,因为书中只有一个模块,而我正试图让 8 个模块一起工作。

这是我的游戏的一小部分:我现在有一个游戏模块、一个棋盘模块和一个输入/输出模块。

我以为我理解何时使用冒号以及何时使用句点,但我不明白为什么在 PIL 示例中,作者将“o”传递给“new”方法以及为什么该方法上有一个冒号?

我的游戏模块应该是我的最高级别模块。在其中,我想我会更新电路板和输入/输出模块并使它们协同工作。但是对于电路板和输入/输出模块来说,这些“新”(意味着初始化)方法是什么样的呢?

这是我一直在使用的一些代码(简化):

Game = {}

function Game:new(o)
  o = {} or o
  setmetatable(o, self)
  self.__index = self
  -- need board and input_output instances below
  o.board = Board:new(o)
  o.input_output = Input_Output:new(o)
  return o
end

return Game
----------------------------------------------------
Board = {}

function Board:new(o)
  o = o or {}
  -- need layout attribute here
  o.layout = { create_layout() }
  setmetatable(o, self)
  self.__index = self
  return o
end

return Board
---------------------------------------------------
Input_Output = {}

function Input_Output:new(o)
  o = o or {}
  -- need read and write attributes here
  o.read = stdin
  o.write = stdout
  setmetatable(o, self)
  self.__index = self
  return o
end

return Input_Output
Run Code Online (Sandbox Code Playgroud)

在像 Ruby 这样的 OOP 语言中,Game 将保存我的 Board 和 Input_Output 类的实例。然后,如果我深入到 game.board,我可以看到板上的公共属性和公共方法。

然而,当我在游戏中更新这两个“类别”时,发生了一些奇怪的事情。我的 self 和 o 变量不是我所期望的(我正在使用 lua_inspect)。我似乎可能会用模块的每个新实例覆盖 o 变量中的数据?

我很迷茫,我认为这是因为“新”方法。我只是不明白。

谁能解释一下吗?我的问题主要是 - 示例中的“o”是什么以及为什么“new”方法上有一个冒号?

Tim*_*Tim 6

我试图解释这一点。这也将帮助我更深入地理解这一点。

因此,只需查看http://www.lua.org/pil/16.1.html中的示例即可。

Account = {}
function Account:new (o)
  o = o or {}   -- create object if user does not provide one
  setmetatable(o, self)
  self.__index = self
  return o
end
Run Code Online (Sandbox Code Playgroud)

理解Lua中的函数

function foo (x) return 2*x end
Run Code Online (Sandbox Code Playgroud)

只是我们所说的语法糖的一个例子;换句话说,这只是一种漂亮的写作方式

foo = function (x) return 2*x end
Run Code Online (Sandbox Code Playgroud)

你知道这:只是 的一个语法工具.

function Account:new (o)
Run Code Online (Sandbox Code Playgroud)

是相同的

function Account.new ( Account, o )
Run Code Online (Sandbox Code Playgroud)

所以最重要的是,我们知道

function Account:new (o)
Run Code Online (Sandbox Code Playgroud)

是相同的

Account.new = function( Account, o )
Run Code Online (Sandbox Code Playgroud)

我们如何在 Lua Table 中找到一些东西

a = Account.new
Run Code Online (Sandbox Code Playgroud)

查找过程如下:

Find if there is a 'new' key in `Account`? --Yes-- Return `Account.new`
               |
               No
               |
Check if `Account` has a metatable? --No-- Return `nil`
                |
                Yes
                |
Check if there is a 'new' key in the `__index` field of `Account`'s metatable --No-- Return `nil`
                |
                Yes
                |
    Assume `metaAccount` is the metatable of Account
    Return `metaAccount.__index.new`
Run Code Online (Sandbox Code Playgroud)

Account:new做什么的

o = o or {} -- just create a new table if the input o is nil

setmetatable(o, self) -- self is Account because of `:`, and set o's metatable to Account

self.__index = self -- the same as Account.__index = Account, this is set the `__index` field of Account, which is o's metatable

return o --return o, a table which has a metatable `Account`, and `Account` has a `__index` field is also `Account`
Run Code Online (Sandbox Code Playgroud)

怎么运行的

我们定义另一个函数

function Account:print() --the same as Account.print = function( self )
    print("Class Account.")
end

a = Account:new() --a is a table which has a metatable `Account`, and `Account` has a `__index` field is also `Account`
a:print() --find a.print in a's metatable `Account` as a function, and run it
-->Class Account.
Run Code Online (Sandbox Code Playgroud)

你现在就该清楚了...