简体   繁体   English

在Lua中进行类型检查的OOP

[英]OOP with type-checking in Lua

I'm trying to implement classes with inheritance in Lua. 我试图在Lua中实现带有继承的类。 Additionally, when creating new objects, there needs to be some kind of "type-checking". 另外,在创建新对象时,需要进行某种“类型检查”。

One particular requirement is to use a specific syntax for creating a class (example): 一个特殊的要求是使用特定的语法创建类(示例):

Class({Volkswagen, Vehicle, model=String, speed=Number, driving=Boolean})

Please note that the parameters are passed as a list with variable length. 请注意,参数以可变长度的列表形式传递。 So I need to have a function that gets an arbitrary number of arguments: 因此,我需要一个可以获取任意数量参数的函数:

function Class (arguments)
    name = arguments[1]
    superclass = arguments[2]
    ...
end

The first two arguments are the name of the class and the name of the class it is inherited from. 前两个参数是类的名称和从其继承的类的名称。

I'm sure the solution is simple, but I have absolutely no idea how to handle the key-value pairs of the attributes, for example... 我确定解决方案很简单,但是例如,我绝对不知道如何处理属性的键值对。

print(arguments.model)
print(type(arguments.model))

...are both nil. 都是零 Any ideas how i can get the type of the attributes (String, Boolean, Number)? 有什么想法可以获取属性类型(字符串,布尔值,数字)吗?

This line: 这行:

{Volkswagen, Vehicle, model=String, speed=Number, driving=Boolean}

Does not mean what you think it means. 不代表您的意思。 It's the values. 是价值。

When you do {Volkswagen} , what you're doing is telling Lua to grab the value currently stored in the global table under the string name "Volkswagen" . 当您执行{Volkswagen} ,您正在做的就是告诉Lua提取当前存储在全局表中的字符串名称为"Volkswagen" I'll assume that there's some value stored there. 我假设那里存储了一些值。

Your problem is that {model=String} is putting the value currently stored in the global table under the string name "String" into the table. 您的问题是{model=String}将当前存储在全局表中的值以字符串名称"String"放入表中。 Odds are good that there is no global variable named "String" . 没有名为"String"全局变量的可能性很好。 Which means you put nil in the table. 这意味着您将nil放在表中。

Unless LuaJava defines them, the "attributes" (String, Boolean, Number) do not exist. 除非LuaJava定义它们,否则“属性”(字符串,布尔值,数字)不存在。 They're just empty values. 它们只是空值。 You can't pass an empty value. 您不能传递空值。 You can pass a string , but that's different. 您可以传递一个字符串 ,但这是不同的。

You didn't explain very well what exactly this Class function of yours should be doing, so I can't give you a hint on how to do what it is you're trying to do better. 您没有很好地解释您的Class函数到底应该做什么,所以我无法向您提示如何做您想做得更好的事情。

If you're just looking for a generic Lua class system, here's my implementation: https://gist.github.com/1722329 如果您只是在寻找通用的Lua类系统,这是我的实现: https : //gist.github.com/1722329

It doesn't support type checking, but that should be easily implemented with a "TypedClass" class to act as a base for all strictly-typed classes. 它不支持类型检查,但应使用“ TypedClass”类轻松实现,以作为所有严格类型的类的基础。

For handling the key value pairs you may need to transfer the referenced table to a local table using the built in table function: 为了处理键值对,您可能需要使用内置表功能将引用表转移到本地表:

function Class(params)
local arguments={}

function transfer(k, v)
  arguments[k]=v
end

--transfer referenced table to local table
table.foreach (params, transfer)

print(arguments[1])
print(arguments[2])
print(arguments.model)
print(arguments.speed)
print(arguments.driving)

end 结束

Class({"Volkswagen","Vehicle", model="" , speed=0 , driving=false} ) 类别({“ Volkswagen”,“ Vehicle”,model =“”,speed = 0,Driving = false})

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

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