Luacocos2d-lua中定义类的四中方法

Luacocos2d-lua中定义类的四中方法,第1张

概述=====Account Start==========local Account = { balance = 0, -- withdraw = function(self, v) -- self.balance = self.balance - v -- end,}function Account : withdraw(v) if v > s
=====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 - vend  function Account : deposit(v)    self.balance = self.balance + vendfunction Account : new(account)    account = account or {}    setMetatable(account,self)    self.__index = self    return accountend--=====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 - vendfunction SpecialAccount : getlimit()    return self.limitend--=====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)endfunction 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)endreturn MainScene
总结

以上是内存溢出为你收集整理的Lua/cocos2d-lua中定义类的四中方法全部内容,希望文章能够帮你解决Lua/cocos2d-lua中定义类的四中方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1243580.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存