简体   繁体   English

Lua 中 OOP 库的设计问题

[英]Design questions for an OOP library in Lua

I implemented a small OOP library in Lua, and two things are not quite right yet.我在 Lua 中实现了一个小型的 OOP 库,有两件事还不太对。 I need your advice!我需要你的建议!

How to call super()?如何调用super()?

I need to make a choice.我需要做出选择。 The three arguments I need to resolve a call to super() are:我需要解决对 super() 的调用的三个参数是:

  • The class from where the call is being made (CallerClass)进行呼叫的类(CallerClass)
  • The instance to be passed (self)要传递的实例(self)
  • The name of the method (method)方法名(method)

I hesitate between these three forms:我在这三种形式之间犹豫不决:

--# Current way:
self:super(CallerClass):method()

--# Variant, which I now find cleaner:
CallerClass:super(self):method()

--# Python style, which is nice too:
super(CallerClass, self):method()

Which one looks nicer and or easier to remember to you?哪一个看起来更好或更容易记住?

Do I need a Class symbol?我需要一个班级符号吗?

In the current version, the only concept is a table named Object , which you can subclass.在当前版本中,唯一的概念是名为Object的表,您可以将其子类化。 I have another version where I introduced a Class symbol.我有另一个版本,我在其中引入了 Class 符号。

Its use is to tell instances from classes.它的用途是从类中区分实例。 Ie : IE :

assert(Object:isKindOf(Class))
local object = Object:new()
assert(not object:isKindOf(Class))

I find it very handy to document and enforce that a method must be called from a class, by starting the method with:我发现记录和强制必须从类调用方法非常方便,方法是通过以下方式启动:

assert(self:isKindOf(Class))

Is it usefull in Lua?它在 Lua 中有用吗? What do you think?你怎么认为? Thanks!谢谢!

--# Python style, which is nice too:
super(CallerClass, self):method()

我会说有一个类对象 - 更多的信息/元数据更好

Thanks Chris, done.谢谢克里斯,完成了。

For those interested, the code is published on the Lua Users Wiki, see ObjectLua .对于那些感兴趣的人,代码已发布在 Lua 用户 Wiki 上,请参阅ObjectLua

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

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