Lua NSE脚本-与在表上使用self混淆';s函数

Lua NSE脚本-与在表上使用self混淆';s函数,lua,Lua,您好,我正在尝试了解更多基于Lua的NSE脚本,但在将函数用作表中的元素时,有一些语法方面的内容我无法理解。我将展示脚本nmap/scripts/broadcast-rip-discver.nse中我迷路的部分: RIPv2 = { -- The Request class contains functions to build a RIPv2 Request Request = { -- Creates a new Request instance

您好,我正在尝试了解更多基于Lua的NSE脚本,但在将函数用作表中的元素时,有一些语法方面的内容我无法理解。我将展示脚本nmap/scripts/broadcast-rip-discver.nse中我迷路的部分:

RIPv2 = {


    -- The Request class contains functions to build a RIPv2 Request
    Request = {

        -- Creates a new Request instance
        --
        -- @param command number containing the RIPv2 Command to use
        -- @return o instance of request
        -- code ommitted (give values to the table o) 
            setmetatable(o, self)
            self.__index = self
            return o
        end,

        -- Converts the whole request to a string
        __tostring = function(self)
            --- -- code ommitted  ( Override the metafunction __tostring) 
            return data
        end,

    },

    -- The Response class contains code needed to parse a RIPv2 response
    Response = {

        -- Creates a new Response instance based on raw socket data
        --
        -- @param data string containing the raw socket response
        -- @return o Response instance
        new = function(self, data)
            local o = { data = data }

            -- code ommitted (Read from data and pass values to o) 

            setmetatable(o, self)
            self.__index = self
            return o
        end,

    }

}
从脚本的“动作”部分,我们有这样的用法

 local rip = RIPv2.Request:new(RIPv2.Command.Request) 
 local response = RIPv2.Response:new(data) -- Data has been already give a value
我知道为这两行创建表RIPv2的新实例“类似”。由于所有的函数都在一个表中(这不是一个类,因为Lua只有一些基本的工具可以使事情与类相似,但与类不同),所以“self”参数对Lua来说是强制性的,它知道该函数的位置

但我不明白的是,为什么要覆盖从表RIPv2到表o的函数,我是说目标有哪些行 可设置元(o,self))

我知道变量表o现在可以有和RIPv2相同的函数,但这部分让我发疯,我在Nmap论坛上找不到一个直接的答案

p.d.声明RiPv2与“local”(确保该变量不是全局变量)的区别是什么

因为所有函数都在一个表中,所以“self”参数是Lua必须知道它的位置

并非所有函数都是表的一部分。元表允许您指定表
A
中的查找可以解析为表
b
中的查找,而这正是这里要做的

但我不明白的是,为什么要尝试覆盖从表RIPv2到表o的函数,我指的是目标有哪些行?:setmetatable(o,self))

o
是类的实例,它只包含实例数据。这些方法存储在类对象
Request
Response
中。元表允许尝试索引
o
以通过类对象解析