Lua/cocos2d-lua中定义类的四中方法

=====Account Start==========
local Account = {
    balance = 0,

  --  withdraw = function(self, v)
   --     self.balance = self.balance - v
  --  end,
}


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(account)
    account = account or {}
    setmetatable(account, self)
    self.__index = self

    return account
end
--=====Account End==========

--=====SpecialAccount Start==========
local 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
end
--=====SpecialAccount End==========


--=====NewAccount Start==========
local NewAccount = {}
function NewAccount : new(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
--=====NewAccount End==========

--=====Test Start==========
local Test = class("Test", Account)
function Test : ctor(name)
    print("get a name:", name)
end

function Test : print()
    print("I'm test class's print function", self.balance)
end
--=====Test End==========
local t = Test : new()
t : withdraw(100)
t : print()


local acc1 = NewAccount : new(100)
acc1.withdraw(40)
print("NewAccount余额", acc1.getBalance())


local s = SpecialAccount : new({limit = 1000})
s : withdraw(200)


local MainScene = class("MainScene", cc.load("mvc").ViewBase)

function MainScene:onCreate()
    -- add background image
    display.newSprite("MainSceneBg.jpg")
        :move(display.center)
        :addTo(self)

    -- add play button
    local playButton = cc.MenuItemImage:create("PlayButton.png", "PlayButton.png")
        :onClicked(function(sender)
            --self:getApp():enterScene("PlayScene")
             --local layer = cc.TestLayer:create()
            -- layer:myPrint("哈哈")

            local a1 = Account : new({balance = 0})
         --   a1:deposit(100)
            --a1.deposit(a1, 100)
           -- getmetatable(a1).__index.deposit(a1, 100)
           -- Account.deposit(a1, 100)
           --a1 : withdraw(100)
            print("余额:", a1.balance)

        end)


    cc.Menu:create(playButton)
        :move(display.cx, display.cy - 200)
        :addTo(self)
end

return MainScene

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值