加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

lua对象类

发布时间:2020-12-14 22:04:21 所属栏目:大数据 来源:网络整理
导读:From: http://www.voidcn.com/article/p-trudwfdh-tb.html 0.lua的类 (1)lua的类实际上就是lua的 table ,类之间的继承实际上就是吧 table 连到一起了,调用方法和属性, ? ? 就是先去第一个table搜索如果没有再去连在后面的table里搜索。 (2)lua里的self实

From: http://www.voidcn.com/article/p-trudwfdh-tb.html

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
? ? ? classA._y = 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 self = {} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?实现了一个类有多个对象,创建一个对象跟谁着创建了一个元表 self,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 如果前面的 local 去掉一个类只能有一个对象
? ? ? ? setmetatable(self,classA) ? ? ??
? ? return self ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
end


4.使用类

classA = {x = 0,y = 0} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?--{}可以声明属性?
classA.__index = classA

function ?classA:new() ? ? ? ? ? ? ? ? ? ? ? ? ? ?--function 前面不能加 local,前面已经通过 classA限定范围了
? ? ? local self = {} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?--创建新的表作为实例的对象
? ? ? setmetatable(self,classA) ? ? ? ? ? ? ?--设置class为对象元表的__index
? ? ? return self ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?--返回该新表
end

function classA:func()
? ? ?print("classA : func")
end

function classA:createA(tag) ? ? ? ? ? ? ? ? ? ? ??
? ? ? print("classA : create ? "..tag)
end


local sa = classA:new() ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?--实例化对象sa
sa:createA(122)
classA:createA(3)


5.类的继承

classB = {}
classB.__index = classB

function ?classB:new() ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?setmetatable(classB,classA) ? ? ? ? ? ? ? ? ?--父类放在后面。实际上就是把 classB 和 classA 连到一起先搜 classB 表有没有这个方法,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?--没有会向后搜索 classA 再没有会继续向后搜索,如果没有会返回 nil

? ? ? return classB ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
end

function classB:createB(tag) ? ? ? ? ? ? ? ? ? ? ??
? ? ?print("classA : create ? "..tag)
end


local sb = classB:new()
sb:createA(102)


6.2dx的lua继承

(1)class 是 cocos2d-x 的方法 path: ../cocos2d-x-3.1/cocos/scripting/lua-bindings/script/extern.lua
? ? class("A",B) A 继承 B,B必须是lua文件里的类
(2)setmetatable(A,B) 是 lua 的继承, A 继承 B
(3)通过 tolua 的继承 tolua.getpeer(target) target:cc.Sprite:create(img) 是C++的一个对象
? ? 再通过 setmetatable(t,ChilSprite) 实现对 C++对象的继承

[plain] view plain copy print ?

  1. local?ParentOne?=?class("ParentOne")???????????????
  2. ??
  3. function?ParentOne:testDemo()??
  4. ????print('ParentOne???????????demo')??
  5. end??
  6. ??
  7. ??
  8. --父类?ParentOne??
  9. local?ChilSprite?=?class("ChilSprite",?ParentOne)??
  10. ChilSprite.__index?=?ChilSprite??
  11. ??
  12. function?ChilSprite.extend(target)??
  13. ????local?t?=?tolua.getpeer(target)?????????????????????????????????--tolua的继承??
  14. ????if?not?t?then??
  15. ????????t?=?{}??
  16. ????????tolua.setpeer(target,?t)??
  17. ????end??
  18. ????setmetatable(t,?ChilSprite)??
  19. ????return?target??
  20. end??
  21. ??
  22. function?ChilSprite:createS(img)??
  23. ????local?sprite?=?ChilSprite.extend(cc.Sprite:create(img))??
  24. ????return?sprite??
  25. end??
  26. ??
  27. function?ChilSprite:myFunc(img)??
  28. ????print("ChilSprite??myFunc???"..img)??
  29. end??
  30. ??
  31. function?ChilSprite:goFunc()??
  32. ????print("ChilSprite??goFunc???")??
  33. end??
  34. ??
  35. ??
  36. --父类?ChilSprite??
  37. local?ChilLayer?=?class("ChilLayer",?ChilSprite)??
  38. ChilLayer.__index?=?ChilLayer??
  39. ??
  40. function?ChilLayer.extend(target)??
  41. ????local?t?=?tolua.getpeer(target)??
  42. ????if?not?t?then??
  43. ????????t?=?{}??
  44. ????????tolua.setpeer(target,?ChilLayer)??
  45. ????return?target??
  46. end??
  47. ??
  48. function?ChilLayer:createL(img)??
  49. ????local?sprite?=?ChilLayer.extend(cc.Sprite:create(img))??
  50. ????return?sprite??
  51. end??
  52. ??
  53. function?ChilLayer:myFunc(img)??
  54. ????print("ChilLayer??myFunc???"..img)??
  55. end??
  56. ??
  57. ??
  58. local?function?testBccLayer(?scene?)??
  59. ????local?ball?=?ChilLayer:createL("Images/ball.png")??
  60. ????ball:setPosition(cc.p(VisibleRect:center().x?-?110,?VisibleRect:center().y?-?110))??
  61. ????ball:setScale(3)??
  62. ????scene:addChild(ball,?1111)??
  63. ??
  64. ????local?cball?=?ChilLayer:createS("Images/ball.png")???????--调用父类的方法??
  65. ????cball:setPosition(cc.p(VisibleRect:center().x?+?110,?VisibleRect:center().y?+?110))??
  66. ????cball:setScale(3)??
  67. ????scene:addChild(cball,?1111)??
  68. ??
  69. ????ball:myFunc("1235")??????????????????????????????????????--调用自己的方法,覆盖父类的方法??
  70. ????ball:goFunc()????????????????????????????????????????????--调用父类的方法??
  71. ????ball:testDemo()??????????????????????????????????????????--调用父类的父类方法??
  72. end??
  73. ??
  74. ??
  75. function?testBcc(scene)??
  76. ????testBccLayer(scene)??
  77. 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");

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读