如何在 Lua 中实现 OO? [英] How can one implement OO in Lua?

查看:34
本文介绍了如何在 Lua 中实现 OO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Lua 没有对 OO 的内置支持,但它允许您自己构建它.能否请您分享一些实现面向对象的方法?

Lua does not have build in support for OO, but it allows you to build it yourself. Could you please share some of the ways one can implement OO?

请为每个答案写一个示例.如果您有更多示例,请发布另一个答案.

Please write one example per answer. If you have more examples, post another answer.

推荐答案

我喜欢将 OOP 视为容器(对象)内的数据封装以及可以使用这些数据完成的操作子集.它还有很多内容,但让我们假设这个简单的定义就是全部,并从中构建一些在 Lua 中的东西(对其他 OO 实现的一些熟悉对读者来说也是一个很好的提升).

I like to think of OOP as being the encapsulation of data inside a container (the Object) coupled with a subset of operations that can be done with this data. There IS a lot more to it, but let's assume that this simple definition is all and build something in Lua from it (also some familiarity with other OO implementations can be a nice boost for the reader).

稍微接触过 Lua 的人都知道,表是一种存储键值对的巧妙方式,结合字符串,事情开始变得非常有趣:

As anyone with a little exposure to Lua may know, tables are a neat way to store key-value pairs and in combination with strings, things start to become very interesting:

local obj = {} -- a new table
obj["name"] = "John"
obj["age"] = 20
-- but there's a shortcut!
print("A person: " .. obj.name .. " of the age " .. obj.age)

在表中作为键的字符串值的访问方式与 C 中结构的成员或 C++/Java 和类似语言中对象的公共成员非常相似.

String values as keys in a table can be accessed in a way very alike to the members of a struct in C or the public members of an object in C++/Java and similar languages.

现在来看一个很酷的魔术:让我们将它与匿名函数结合起来.

And now for a cool magic trick: let's combine this with anonymous functions.

-- assume the obj from last example
obj.hello = function () 
   print("Hello!")
end

obj.goodbye = function ()
   print("I must be going.")
end

obj.hello()
obj.goodbye()

很棒吧?我们现在可以将函数存储在我们的表中,您可以再次看到它类似于其他 OOP 语言中方法的使用方式.但是缺少一些东西.我们如何在方法定义中访问属于我们对象的数据?这通常通过将表中函数的签名更改为如下所示来解决:

Awesome right? We now have means of having functions stored inside our tables, and again you can see it resembles how methods are used in other OOP languages. But something is missing. How can we access the data that belongs to our object inside our method definitions? This is generally addressed by changing the signature of the functions in the table to something like this:

-- assume the obj from last example
obj.inspect = function (self)
   print("A person: " .. self.name .. " of the age " .. self.age)
end

obj.hello = function (self) 
   print(self.name .. ": Hello! I'm " .. self.name)
end

obj.goodbye = function (self)
   print(self.name .. ": I must be going.")
end

-- now it receives the calling object as the first parameter
obj.inspect(obj) -- A person: John of age 20
obj.hello(obj) -- John: Hello! I'm John
obj.goodbye(obj) -- John: I must be going

这以一种简单的方式解决了它.也许与 Python 中的工作方式(方法总是有一个明确的自我)类似,可以帮助您了解它在 Lua 中是如何工作的.但是男孩,在我们的方法调用中显式传递所有这些对象不是很不方便吗?是的,它也困扰着我,所以还有另一个快捷方式可以帮助您使用 OOP:

That solves it in a simple manner. Maybe drawing a parallel to the way things work in Python (methods always get a explicit self) can aid you in learning how this works in Lua. But boy, isn't it inconvenient to be passing all these objects explicitly in our method calls? Yeah it bothers me too, so there's another shortcut to aid you in the use of OOP:

obj:hello() -- is the same as obj.hello(obj)

最后,我只是触及了如何做到这一点的表面.正如凯文·维米尔的评论中所述,Lua 用户 Wiki 是有关此主题的绝佳信息来源,您可以在那里了解如何实现在这个答案中被忽略的 OOP 的另一个重要方面(私有成员,如何构造对象,继承,......).请记住,这种做事方式是 Lua 哲学的一小部分,它为您提供了能够构建更高级结构的简单正交工具.

Finally, I have just scratched the surface of how this can be done. As has been noted in Kevin Vermeer's comment, the Lua Users Wiki is an excellent source of information about this topic and there you can learn all about how to implement another important aspects of OOP that have been neglected in this answer (private members, how to construct objects, inheritance, ...). Have in mind that this way of doing things is a little part of the Lua philosophy, giving you simple orthogonal tools capable of building more advanced constructs.

这篇关于如何在 Lua 中实现 OO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆