Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop Lua面向对象编程-我不知道';I don’我不知道如何新建一个“a”的实例;“类”;?_Oop_Lua - Fatal编程技术网

Oop Lua面向对象编程-我不知道';I don’我不知道如何新建一个“a”的实例;“类”;?

Oop Lua面向对象编程-我不知道';I don’我不知道如何新建一个“a”的实例;“类”;?,oop,lua,Oop,Lua,我正在构建一个简单的游戏,我一直在遵循PIL手册。以下是我的游戏场景: 我很挣扎,因为书中只有一个模块,我试图让8个模块一起工作 这里是我游戏的一小部分:我现在有一个游戏模块、一个棋盘模块和一个输入/输出模块 我想我理解了何时使用冒号和何时使用句号,但我不理解为什么在PIL示例中,作者将“o”传递给“new”方法,以及为什么该方法上有冒号 我的游戏模块应该是我的最高级别模块。在它里面,我想我会重新设计电路板和输入/输出模块,让它们一起工作。但是,对于电路板和输入/输出模块,这些“新”(意思是初

我正在构建一个简单的游戏,我一直在遵循PIL手册。以下是我的游戏场景:

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

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

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

我的游戏模块应该是我的最高级别模块。在它里面,我想我会重新设计电路板和输入/输出模块,让它们一起工作。但是,对于电路板和输入/输出模块,这些“新”(意思是初始化)方法看起来是什么样的呢

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

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

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

我很迷茫,我想这是因为“新”的方法。我就是不明白

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

事实上,Lua中没有“官方”类,所以您可以以任何方式实现它们。在这个特定的实现中,
o
是新的实例,可能带有一些预定义的参数。它继承父级的所有成员,无论是值还是方法-函数-这在Lua中都是一样的。如果您在此调用中根本不提供
o
,将为您创建一个空表

函数定义中的
只是
函数游戏的语法糖。新的(self,o)
-也就是说,它添加了一个名为
self
的第一个参数

您应该像调用本地my\u game=game:new()一样调用此函数。
。之后,
myu-game
将是一个包含
myu-game.board
myu-game.input\u-output
成员的表


你现在的问题不太清楚你看到了什么,它与你期望的有什么不同。如果你提供更多的细节,我也可以添加更多的细节来回答。

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

因此,请看中的示例

理解Lua中的函数 只是我们称之为语法糖的一个例子;换句话说,这只是一种很好的写作方式

foo = function (x) return 2*x end

您知道,
只是
的语法工具

function Account:new (o)
function Account:new (o)

function Account.new ( Account, o )
Account.new = function( Account, o )

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

function Account.new ( Account, o )
Account.new = function( Account, o )
我们如何在Lua表中找到一些东西 调查过程如下:

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
做什么 工作原理 我们定义另一个函数

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.
您现在应该清除…

以便重点关注“示例中的'o'是什么,为什么在“new”方法上有冒号?”:

所以游戏就是这个类:它是一个带有函数的表。这是Lua中类的基本概念。那么,什么是游戏的实例呢?它是一个表,其元表是其类的表,即游戏

但在Lua中,表中的函数不能知道它在哪个表中,除非它被作为调用参数提供此信息。当您用冒号而不是圆点调用它时,Lua会自动将包含表的引用作为函数的第一个参数,从而简化此操作。第一个参数的名称为“self”。因此,在“new”函数中,用冒号调用它提供了表,new在表中定义为self-param,即类游戏


因此,您可以看到“o”是您正在创建的实例,您可以使用冒号调用new,因为否则您必须给它一个类表,该类表将在您的语句中出现两次,即冒号提供了更好的语法。

作为旁注,有相当多的OOP实现(例如)。您可以使用这些来代替自己管理元表。非常感谢!我现在明白“o”是不必要的。我认为是这样的,对此我感到困惑,所以我选择了你的答案。
o
可以用作初始化,如果你想预定义一些类的话。假设你的
游戏
类有
等级
,默认为
0
。您可以使用
local my_game=game:new({level=3})
初始化它,之后
my_game.level
将从一开始就是
3
。非常感谢您的帮助!你的回答也帮助我解决了其他一些我感到困惑的问题。
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`
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.