本文介绍了Lua面向对象的编程-我不了解如何新建“类"的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building a simple game and I've been following the PIL book. Here is the scenario I am basing my game off of:http://www.lua.org/pil/16.1.html

我很努力,因为书中只有一个模块,而我正试图使8个模块一起工作.

I'm struggling because there is only one module in the book and I'm trying to get 8 modules working together.

这只是我游戏的一小部分:我现在有一个Game模块,一个Board模块和一个Input/Output模块.

Here's a small part of my game: I have a Game module, a Board module and a Input/Output module for now.

我以为我了解何时使用冒号和何时使用句号,但是我不明白为什么在PIL示例中作者将"o"传递给"new"方法以及为什么会有冒号在那种方法上?

I thought I understood when to use colon and when to use period, but I'm not understanding why, in the PIL example, the author is passing in "o" to the "new" method and why there is a colon on that method?

我的游戏模块应该是我最高级别的模块.在其中,我想我将更换电路板和输入/输出模块并使它们一起工作.但是,板子和输入/输出模块的那些新"(意味着初始化)方法是什么样的?

My Game module is supposed to be my highest level module. In it, I would think I would new up the board and input/output modules and get those working together. But what do those "new" (meaning initialize) methods look like for the board and input/output modules?

这里有一些我正在使用的代码(简体):

Here some code I've been working with (simplified):

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,我可以看到板上的公共属性和公共方法.

In an OOP language like Ruby, Game would hold instances of my Board and Input_Output classes. Then if I drilled down to game.board, I could see the public attributes and public methods on board.

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

However, when I am newing up these 2 "classes" in game, something weird is going on. My self and o variables are not what I would expect them to be (I'm using lua_inspect). I seem to maybe be overwriting data in the o variable with each new instance of a module?

我很迷路,我认为这是因为采用了新"方法.我只是不明白.

I'm so lost and I think it's because of the "new" method. I'm just not getting it.

有人可以解释吗?我的问题主要是-示例中的"o"是什么,为什么"new"方法上会有一个冒号?

Can anyone explain? My questions mainly are - what's with the "o" in the example and why is there a colon on the "new" method?

推荐答案

实际上,Lua中没有官方"类,因此您可以根据需要实现它们.在此特定实现中,o是新实例,可能带有一些预定义的参数.它继承了parent的所有成员,无论它们是值还是方法-函数-在Lua中都是一样的.如果您根本没有在此调用中提供o,将为您创建一个空表.

Actually there's no "official" classes in Lua, so you can implement them any way you want. In this particular implementation o is the new instance, possibly with some pre-defined parameters. It inherits all members of parent, be they values or methods - functions - that's all the same in Lua. If you do not supply an o in this call at all, an empty table will be created for you.

:只是function Game.new(self, o)的语法糖-也就是说,它添加了一个名为self的第一个参数.

: in function definition is just a syntactic sugar for function Game.new(self, o) - that is, it adds a very first parameter called self.

您应该像local my_game = Game:new()一样调用此函数.之后,my_game将成为具有my_game.boardmy_game.input_output成员的表.

You should call this function like local my_game = Game:new(). After that, my_game will be a table that have my_game.board and my_game.input_output members.

关于您看到的内容以及与您的期望有何不同,您当前的问题尚不十分清楚.如果您提供更多详细信息,我也可以添加更多详细信息来回答.

Your current question is not exactly clear on what do you see and how it is different from what you expect. If you provide more details, I can add more details to answer too.

这篇关于Lua面向对象的编程-我不了解如何新建“类"的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 02:00