简体   繁体   English

如何使用Lua Metatables实现OO?

[英]How do I implement OO with Lua Metatables?

I've never been able to fully wrap my mind about how Lua uses metatables to implement Object Orientation in its programming. 关于Lua如何在编程中使用元表实现对象定向,我从来没有完全下定决心。 I've been reading over the online tutorial directory trying to understand, however the author wrote it with experienced programmers in mind, in which I am not. 我一直在阅读在线教程目录,试图理解它,但是作者在编写本教程时就考虑了经验丰富的程序员,但我不是。 With abstract ideas and not-noob-friendly variables and explanations, going's been a little tough. 有了抽象的想法以及对菜鸟不友好的变量和解释,事情变得有些艰难。 Here is a link to the Object Orientation page on Lua's website. 这是Lua网站上“对象定向”页面的链接。

Can anyone give a noob-friendly explanation of how to implement it? 谁能给菜鸟一个友好的解释如何实现它? I'm doing some scripting for a game I play (not WoW), and I'd like to make my code better by using it. 我正在为自己玩的游戏(不是《魔兽世界》)编写一些脚本,我想通过使用它来使代码更好。 Thank you! 谢谢!

There are many ways to code up an OO feel in Lua. 有很多方法可以在Lua中编写面向对象的感觉。 Lua goes for mechanism, not policy. Lua追求机制,而不是政策。 The best possible source is the chapter in Roberto's book on Object-Oriented Programming . 最好的资料来源是Roberto关于面向对象编程的书中章节 Definitely noob-friendly. 绝对是菜鸟友好。 Great book. 很棒的书。 The link is to the previous edition, which is free online. 链接是上一个版本的,该版本可以免费在线获得。 But you will want to buy the latest edition :-) 但是,您将需要购买最新版本:-)

Disclaimer: I'm the developer of MiddleClass 免责声明:我是MiddleClass的开发人员

I've developed a library for people like you. 我已经为像你这样的人开发了一个图书馆。 It's called MiddleClass . 它称为MiddleClass

It conveniently hides the metatables so you can concentrate on object-oriented stuff. 它方便地隐藏了元表,因此您可以专注于面向对象的东西。 It's also reasonably commented out, should you want to take a look underneath. 如果您想在下面查看,它也会被合理地注释掉。

There's also a wiki page explaining how to use it. 还有一个Wiki页面,说明如何使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM