• Chapter 16_2 继承


      类也是对象,所有它们也可以从其他类获得方法。这就是“继承”,可以在Lua中表示:

    Account = { balance = 0}
    function Account:new(o)
        o = o or {}
        setmetatable(o , self)
        self.__index = self
        return o
    end
    function Account:deposit(v)
        self.balance = self.balance + v
    end
    function Account:withdraw( v)
        if v> self.balance then error "infufficient funds" end
        self.balance = self.balance - v
    end

    如果想从这个类中派生一个子类,以使客户能够透支。则先需要创建一个空的类,从基类继承所有的操作:

    SpecialAccount = Account:new()

    SpecialAccount就是一个Account的实例。如下所示:

    s  = SpecialAccount:new(limit = 1000.00)

    SpecialAccount从Account继承了new方法。它的self参数表示为SpecialAccount。当执行:

    s:deposit(100.00)

    Lua在s中不能找到deposit字段,就会查找SpecialAccount,如果还找不到,就查找Account。最终会在那里找到deposit的原始实现。

    SpecialAccount之所以特别,是因为可以重定义那些从基类继承的方法。编写一个方法的新实现只需:

    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:withdraw(200.00)时,Lua就不会在Account中找了,因为它有了。由于s.limit为1000.00,程序会执行取款,并使s变成一个负的余额。

      Lua中的对象有一个特殊现象,就是无须为指定一种新行为而创建一个新类。如果只有一个对象需要某种特殊的行为,那么可以直接在该对象中实现这个行为。

    例如账户s表示一个特殊客户,这个客户的透支额度是其余额的10%,那么可以只修改这个对象:

    function s:getLimit()
        return self.balance * 0.10
    end

    调用s:withdraw(200.00)还是会执行SpecialAccount的withdraw。但withdraw调用的self:getLimit则是上面的这个定义。

    以上内容来自:《Lua程序设计第二版》和《Programming in Lua  third edition 》

  • 相关阅读:
    pytorch获取模型某一层参数名及参数值方式
    PyTorch源码解读之torchvision.models
    torch.nn.Conv2d()函数详解
    UCF101
    ipynb格式转换
    linux下的压缩与解压缩
    tomcat 部署的项目图片名字带中文
    恢复oracle误删除数据
    项目中遇到的分布式高并发情况及解决方案
    poi 导出复杂表头 execl
  • 原文地址:https://www.cnblogs.com/daiker/p/5882578.html
Copyright © 2020-2023  润新知