문제

I am trying to create a new class which inherits from another class someone else made, but I don't understand the way the class is made, and can't figure out how to make my new class.

Each class is in a seperate .lua file.

The base class looks like this:

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

Then what I think is a subclass is created from the base class like so:

    local Base = require( "vyzor.base" )

    local MiniConsole = Base( "Component", "MiniConsole" )

    local function new (_, name, init_x, init_y, init_width, init_height, word_wrap, font_size)
        local new_console = {}
        <other local variables>

        local function updateAbsolutes ()
            <does stuff>
        end
    local properties = {
    Name = {
        get = function ()
            return name
        end
    },
    Container = {
        get = function ()
            return container
        end,
        set = function (value)
            if value.Type == "Frame" then
                container = value
            end
        end,
                     <and so on>
        end

    function new_console:Echo (text)
         echo( name, text )
     end
    <other functions>

setmetatable( new_console, {
    __index = function (_, key)
        return (properties[key] and properties[key].get()) or MiniConsole[key]
    end,
    __newindex = function (_, key, value)
        if properties[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 = new,
} )
return MiniConsole

How would I make a class from the MiniConsole class that for example has a new function and variable, and also shares the same properties table from Miniconsole with some more additions:

I think it should start something like this?

    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)

Everything I have tried ends up giving me a "circular require or other error" error when I try to pcall it to use it elsewhere.

도움이 되었습니까?

해결책

You can add functions and data to any table. So you could do

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

yourMiniConsole = newDerivedMC()

If you access a field (data or function) that does not exist in yourMiniConsole, interpreter will look in miniConsole to see if it exists. If you set a field that does not exist, same thing.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top