lua 类、继承、多重继承

--[[
lua是没有类的,但是可以用元表模仿类和类的继承
--]]
--@file Account.lua
--@desc 类Account
local Account = {
    balance = 0
}

function Account:new(o)
    o = o or {}
    --lua使用元表实现类,继承
    setmetatable(o, self)
    self.__index = self
    return o
end

--等价于function Account.deposit(self, v)
function Account:deposit( v )
    print("user deposit", v)
    self.balance = self.balance + v
end

function Account:withdraw( v )
    print("user withdrwa", v)
    if v > self.balance then
        error "insufficient funds"
    end
    self.balance = self.balance - v
end

function Account:getBalance(  )
    print("user have balance", self.balance)
    return self.balance
end

return Account

-----------------------------------------------
--@file SpecialAccount
--@desc 类SpcialAccount,继承类Account
local Account = require("Account")
local SpecialAccount = {
    limit = 1000
}

function SpecialAccount:new( o )
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    Account:new(self)
    return o
end

function SpecialAccount:deposit( v )
    local curr = self.balance + v
    if curr > self:getLimit() then
        error "outnumber limit"
    end
    self.balance = curr
end

function SpecialAccount:getLimit(  )
    print("user limit is", self.limit)
    return self.limit or 0
end

return SpecialAccount

-----------------------------------------
--@file User.lua
--@desc 类User
local User = {
    name = ""
}

function User:new( o )
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    return o
end

function User:setName( name )
    print("user change name")
    self.name = name
end

function User:getName( name )
    print("user name is:", self.name)
    return self.name
end

return User

--------------------------------------------
--@file CreateClass.lua
--@desc 提供createClass方法,该方法可实现多重继承
package.path = 'F:/workspace/Lua/?.lua;' --读取其他路径目录下的lua文件
local dump = require("dump")

local function search( key, list )
    for i = 1, table.getn(list) do
        if type(list[i]) == "table" then
            local value = list[i][key]
            if value then
                return value
            end
        end
    end
end

--可变参数“...”和“arg”
local function createClass( ... )
    local c = {}
    setmetatable(c, {__index = function ( t, k )
        return search(k, arg)
    end})
    c.__index = c
    function c:new( o )
        o = o or {}
        setmetatable(o, self)
        self.__index = self
        return  o
    end
    return c
end

return createClass

-----------------------------------------
--@file AccountUser.lua
--@desc 类AccountUser,继承类Account和类User
local Account = require("Account")
local User = require("User")
local class = require("CreateClass")

local AccountUser = class(Account, User)

function AccountUser:getName(  )
    print("AccountUser getName:", self.name)
    return self.name
end

return AccountUser

---------------------------------------------------
--------------- test ---------------------------------------
local Account = require("Account")
local SpecialAccount = require("SpecialAccount")
local AccountUser = require("AccountUser")

function main(  )
    ----------------- test class -----------------------------
    local classTest = Account:new()
    classTest:deposit(5)
    classTest:withdraw(4)
    classTest:getBalance()
    --------------- test inherit ----------------------------------
    -- SpecialAccount inherit Account
    local inheritTest = SpecialAccount:new({limit = 10})
    inheritTest:getLimit()
    inheritTest:getBalance()
    inheritTest:deposit(5)
    inheritTest:getBalance()
    inheritTest:deposit(10)
    inheritTest:getBalance()
    --------------- test multiple inheritance --------------------
    -- AccountUser inherit Account and User
    local mulInheritTest = AccountUser:new()
    mulInheritTest:setName("huang")
    mulInheritTest:getName()
    mulInheritTest:deposit(50)
end

main()



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值