理解现有的“lua”类并继承它。

我正在尝试创建一个继承别人创建的另一个类的新类,但我不理解这个类的制作方式,也无法弄清如何制作我的新类。

每个类都在单独的“。lua”文件中。

基类看起来像这样:

local Base = {}
local function new (_, _type, _subtype)
    local new_base = {}
    local properties = {
    Type = {
        get = function ()
            return _type
        end
        },
    Subtype = {
        get = function ()
            return _subtype
        end
        }
    }

setmetatable( new_base, {
    __index = function (_, key)
        return (properties[key] and properties[key].get()) or Base[key]
    end,
    __newindex = function (_, key, value)
        if properties[key] and properties[key].set then
            properties[key].set( value )
        end
    end,
    } )
return new_base
end

setmetatable( Base, {
__call = new,
} )
return Base

然后我认为一个子类从基类创建如下:

    local Base = require"vyzor.base")

    local MiniConsole = Base(“ Component”,“ MiniConsole”)

    local function new_nameinit_xinit_yinit_widthinit_heightword_wrapfont_size)
        local new_console = {}
        <其他本地变量>

        local function updateAbsolutes()
            <完成任务>
        end
    local properties = {
    Name = {
        get = function ()
            return name
        end
    },
    Container = {
        get = function ()
            return container
        end,
        set = functionvalue)
            if value.Type ==“ Framethen
                container = value
            end
        end,
                 <等等>
        end

    function new_consoleEchotext)
         echonametext)
     end
    <其他功能>

setmetatablenew_console,{
    __index = function_key)
        returnproperties[key] and properties[key].get())or MiniConsole[key]
    end,
    __newindex = function_keyvalue)
        if属性[key] and properties[key] .set then
            properties[key].set(value)
        end
    end,
    })
    master_list [name] = new_console
    return new_console
end

setmetatable( MiniConsole,{
    __index = getmetatable(MiniConsole).__index,
    __call =新的,
})
return MiniConsole

如何从MiniConsole类创建一个类,例如具有新的函数和变量,并使用Miniconsole的属性表共享一些更多添加:

我认为它应该像这样开始?

    local function new(_,name,title,init_x,init_y,init_width,init_height,word_wrap,font_size)
        local new_console = MiniConsole(name,init_x,init_y,init_width,init_height,word_wrap,font_size)

我尝试的所有内容最终都会在我尝试在其他地方使用pcall时给我“循环require或其他错误”错误。

点赞
用户869951
用户869951

你可以向任何表格添加函数和数据。因此,你可以这样做:

function newDerivedMC()
    miniConsole = MiniConsole(...)
    derived = {
        yourData = 1,
        yourMethod = function(self, ...) ... end,
    }
    setmetatable(derived, miniConsole)
    return derived
end

yourMiniConsole = newDerivedMC()

如果你访问 yourMiniConsole 中不存在的字段(数据或函数),解释器将查看 miniConsole 中是否存在。如果你设置一个不存在的字段,同样也是这样。

2014-05-16 04:26:16