lua优雅的实现继承,栈队列实例

  • 实现方式来源网络
function class(base, _ctor)
    local c = {}    -- a new class instance
    if not _ctor and type(base) == 'function' then
        _ctor = base
        base = nil
    elseif type(base) == 'table' then
    -- our new class is a shallow copy of the base class!
    -- 只能继承全局变量,local变量需要转换为类方法来继承
        for i,v in pairs(base) do
            c[i] = v
            print(i,v)
        end
        -- c.__index = base
        c._base = base
    end

    -- the class will be the metatable for all its objects,
    -- and they will look up their methods in it.
    c.__index = c

    -- expose a constructor which can be called by ()
    local mt = {}

    mt.__call = function(class_tbl, ...)
        local obj = {}
        setmetatable(obj,c)
        -- 调用call的时候调用构造函数
        if _ctor then
            _ctor(obj,...)
        end
        return obj
    end

    c._ctor = _ctor
    c.is_a = function(self, klass)
        local m = getmetatable(self)
        while m do
            if m == klass then return true end
            m = m._base
        end
        return false
    end
    setmetatable(c, mt)
    return c
end
  • 基本使用
require("object")

Animal = class(function( self,name )
    -- 需要继承的全局变量需要写到这里,在子类调用_ctor方法的时候,加入到子类的table
    self.name = name
    self.s = {}
end)


function Animal:add(key,value)
    -- body
    self.s[key]=value
end
function Animal:get(key)
    -- body
    return self.s[key]
end
-- 执行完这段代码后 Animal中所有数据都已经拷贝到Cat中,Animal改变不会引发Cat改变
-- 如果源码中不是拷贝Animal中数据到Cat中,Animal发生变化。Cat将会跟随着一直发生变化
Cat = class(Animal,function( self,name )
    -- body
    Animal._ctor(self,name) --继承父类构造方法(这里是调用Animal._ctor方法,参数是Cat,name)

end)


cat = Cat("cat") --实例化会调用_ctor构造函数
cat:add("x","cat")
cat2 = Cat("cat2")
cat2:add("x","cat2")


print(cat:get("x"))
lua优雅的实现继承,栈队列实例_第1张图片
image.png
  • 队列
require("class")
Queue = class(function(self,capacity)
    -- body
    self.capacity = capacity
    self.queue = {}
    self.size = 0
    self.head = -1
    self.rear = -1
end)

function Queue:enQueue(element)
    if not element then return end
    if self.size == 0 then
        self.head = 0
        self.rear = 0
    end
    if self.capacity then
        if self.size >= self.capacity then
            print("队列已满")
            return 
        end
        self.rear = (self.rear) % self.capacity + 1
    else
        self.rear = self.rear + 1
    end
    self.queue[self.rear] = element
    self.size = self.size + 1
end


function Queue:deQueue()
    if self.size == 0 then
        print("队列为空")
        return nil
    end
    if self.capacity then
        self.head = (self.head) % self.capacity + 1
    else
        self.head = self.head + 1
    end
    self.size = self.size - 1
    return self.queue[self.head]
end
require("class")
Stack = class(function(self)
    self.stack = {}
end)

function Stack:push(element)
    if not element then return end
    table.insert(self.stack,element)
end

function Stack:pop()
    local len = #self.stack
    if len == 0 then
        print("空栈")
        return nil
    end
    local value = self.stack[len]
    self.stack[len] = nil
    return value
end

你可能感兴趣的:(lua优雅的实现继承,栈队列实例)