lua对象类
0.lua的类 (1)lua的类实际上就是lua的 table ,类之间的继承实际上就是吧 table 连到一起了,调用方法和属性, ? ? 就是先去第一个table搜索如果没有再去连在后面的table里搜索。 (2)lua里的self实际上就是table了,也能代表类名 (3)lua继承 local self = {} setmetatable(self,classA) ? ? ? ? ? ? ? ? ? ? ?在表self基础上建立classA,classA是一个新表 setmetatable(classB,classA) ? ? ? ? ? ? ? ? ?在表classA基础上建立classB,classB是一个新表 (4)表唯一的标示 classA.__index = classA 1.类的属性 (1) classA = {x = 0,y = 0} (2) classA = {} ? ? ? classA._x = 0 两种声明属性都行,之后在方法里可以通过,self.x 调用 2.类的方法 function ?classA:new()? ?? ? ? 隐藏传递 self ,默认第一个参数,可以通过 self.xxx 调用属性和方法 end function ?classA.new()? ? ? ? ? 必须要传递 类名(self), 否则不能调用 self end 声明和调用方法都用 :、属性的调用全部用点号 . 3.创建类 classA = {}? classA.__index = classA ? ? function ?classA:new() ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 如果前面的 local 去掉一个类只能有一个对象 4.使用类 classA = {x = 0,y = 0} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?--{}可以声明属性? local sa = classA:new() ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?--实例化对象sa 5.类的继承 classB = {} local sb = classB:new() 6.2dx的lua继承 (1)class 是 cocos2d-x 的方法 path: ../cocos2d-x-3.1/cocos/scripting/lua-bindings/script/extern.lua
local ParentOne = class("ParentOne") function ParentOne:testDemo() print('ParentOne demo') end --父类 ParentOne local ChilSprite = class("ChilSprite",ParentOne) ChilSprite.__index = ChilSprite function ChilSprite.extend(target) local t = tolua.getpeer(target) --tolua的继承 if not t then t = {} tolua.setpeer(target,t) end setmetatable(t,ChilSprite) return target end function ChilSprite:createS(img) local sprite = ChilSprite.extend(cc.Sprite:create(img)) return sprite end function ChilSprite:myFunc(img) print("ChilSprite myFunc "..img) end function ChilSprite:goFunc() print("ChilSprite goFunc ") end --父类 ChilSprite local ChilLayer = class("ChilLayer",ChilSprite) ChilLayer.__index = ChilLayer function ChilLayer.extend(target) local t = tolua.getpeer(target) if not t then t = {} tolua.setpeer(target,ChilLayer) return target end function ChilLayer:createL(img) local sprite = ChilLayer.extend(cc.Sprite:create(img)) return sprite end function ChilLayer:myFunc(img) print("ChilLayer myFunc "..img) end local function testBccLayer( scene ) local ball = ChilLayer:createL("Images/ball.png") ball:setPosition(cc.p(VisibleRect:center().x - 110,VisibleRect:center().y - 110)) ball:setScale(3) scene:addChild(ball,1111) local cball = ChilLayer:createS("Images/ball.png") --调用父类的方法 cball:setPosition(cc.p(VisibleRect:center().x + 110,VisibleRect:center().y + 110)) cball:setScale(3) scene:addChild(cball,1111) ball:myFunc("1235") --调用自己的方法,覆盖父类的方法 ball:goFunc() --调用父类的方法 ball:testDemo() --调用父类的父类方法 end function testBcc(scene) testBccLayer(scene) end 7.通过 class 继承 c++对象 具体可以看 extern.lua 里对继承 lua object 和 c++ object 进行了判断 testCcc = {}; testCcc = class("testCcc",function(img) return cc.Sprite:create(img) testCcc.__index = testCcc; local ccc = testCcc.new("Images/ball.png"); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |