lua学习笔记之面向对象编程

1、类

基于原型。lua使用继承的思想即__index元方法来实现原型。

Account = {balance = 0}

function Account:withdraw(v)
    self.balance = self.balance - v
end

function Account:deposit(v)
    self.balance = self.balance + v
end

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

b = Account:new()
print(b.balance)
b:deposit( 200.00)
print(b.balance)
b:withdraw(100.00)
print(b.balance)

输出为:
0
200.0
100.0

2、继承

通过基类的new来创建基类的实例,然后实例再次new创建子类实例,通过重写实体方法来覆盖基类方法

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by wl.
--- DateTime: 2021/12/2 0:01

Account = {balance = 0}

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

function Account:deposit(v)
    self.balance = self.balance + v
end

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


SpecialAccount = Account:new()

function SpecialAccount:withdraw(v)
    if v - self.balance >= self:getLimit() then
        error "insufficient funds"
    end
    self.balance = self.balance - v
end

function SpecialAccount:getLimit()
    return self.limit or 0
end

s = SpecialAccount:new{limit = 1000.00}
print(s.balance)
s:deposit( 200.00)
print(s.balance)
s:withdraw(100.00)
print(s.balance)

3、多继承

原理仍然是使用元表,只是其__index中是在多个父类中查找。

local function search(k, plist)
    for i = 1, #plist do
        local v = plist[i][k]
        if v then return v end
    end
end

function createClass(...)
    local c = {}
    local parents = {...}
    setmetatable(c, {__index = function(t,k)
        return search(k, parents)
    end})

    c.__index = c

    function c:new(o)
        o = o or {}
        setmetatable(o, c)
        return o
    end

    return c
end

Account = {balance = 0}

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

function Account:deposit(v)
    self.balance = self.balance + v
end

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

Named = {}
function Named:getName()
    return self.name
end

function Named:setName(n)
    self.name = n
end

NamedAccount = createClass(Named, Account)
account = NamedAccount:new{name = "Paul"}
print(account:getName())

 一种优化方式是每次访问时,如果找到放到当前类中。缺点是如果对父类有修改,不会传递到子类中

setmetatable(c, {__index = function(t,k)
        local v =  search(k, parents)
        t[k] = v
        return v
    end})

4、信息隐藏

通过闭包来实现。通过两张表,一张记录属性,一张记录操作。

function newAccount(initialBalance)
    local self = {balance = initialBalance}

    local withdraw = function(v)
        self.balance = self.balance - v
    end

    local deposit = function(v)
        self.balance = self.balance + v
    end

    local getBalance = function()
        return self.balance
    end

    return {
        withdraw = withdraw,
        deposit = deposit,
        getBalance = getBalance
    }
end

acc1 = newAccount(100.00)
acc1.withdraw(40.00)
print(acc1.getBalance())

输出为:
60.0

5、单方法

作为信息的分发

function newObject(value)
    return function(action, v)
        if action == "get" then return value
        elseif action == "set" then value = v
        else error("invalid action")
        end
    end
end

d = newObject(0)
print(d("get"))
d("set", 10)
print(d("get"))

输出:
0
10

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值