Lua/cocos2d-lua中定义类的四中方法
2016-01-08 13:52
441 查看
=====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
相关文章推荐
- Cocos2D瓦块地图高清屏(retina)显示比例问题的解决
- Cocos2D瓦块地图高清屏(retina)显示比例问题的解决
- Cocos2D瓦块地图高清屏(retina)显示比例问题的解决
- 将Cocos项目部署到Eclipse工程中
- Cocos输出中文字符
- cocos2d-x 精灵的创建和基本使用
- cocos2d-x源码
- 安装、配置、部署Cocos2d-x
- cocos2d-3.8.1版本tableview的使用(怎么取消弹簧效果);
- COCOS学习笔记--坐标系统与坐标系转换
- Cocos2d-x利用jni调用java层代码
- cocos jpg alpha_mask png
- cocos2d-x Undefined symbols for architecture x86_64 when porting iOS app in Xcode? 问题的解决
- Cocos2d-x环境搭建
- 关于Can't find Class org.cocos2dx.cpp.AppActivity的解决
- Cocos2d-x 多分辨率适配完全解析
- libuv在cocos2d-x中的使用
- COCOS学习笔记--Cocod2dx内存管理(三)-Coco2d-x内存运行原理
- Mac平台的Cocos2dx开发环境搭建
- 23.cocos2d-x骨骼动画