gpt4 book ai didi

oop - Lua 面向对象编程 - 我不明白如何新建 "class"的实例?

转载 作者:行者123 更新时间:2023-12-02 07:31:35 25 4
gpt4 key购买 nike

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

我正在努力,因为书中只有一个模块,而我正试图让 8 个模块一起工作。

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

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

我的游戏模块应该是我的最高级别模块。在其中,我想我会更新电路板和输入/输出模块并让它们一起工作。但是对于板和输入/输出模块,这些"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

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

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

我很迷茫,我认为这是因为"new"方法。我就是不明白。

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

最佳答案

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

所以,看看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

理解Lua中的函数

function foo (x) return 2*x end

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

foo = function (x) return 2*x end

而且您知道 : 只是 . 的句法工具。

function Account:new (o)

相同

function Account.new ( Account, o )

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

function Account:new (o)

相同

Account.new = function( Account, o )

我们如何在 Lua 表中查找内容

a = Account.new

查找过程如下:

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`

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`

工作原理

我们定义另一个函数

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.

你现在应该清除...

关于oop - Lua 面向对象编程 - 我不明白如何新建 "class"的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21058878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com