碼農往事
          我的代碼為什么這么丑?一定是因為我長的不好看
          posts - 29,comments - 86,trackbacks - 0
          最近的工作主要涉及LUA,這有個坑必須記一下。
          下面是一個LUA面向對象寫法非常常見的寫法。
          Bird = {
              color = {};canFly = true
          }

          function Bird:new(o)
              o = o or {}
              setmetatable(o, self)
              self.__index = self
              self.color = {}
              return o
          end
          注意,這里Bird類有兩個屬性,1個表,1個是基本類型,然后上測試代碼(Utils類只是簡單的封裝類,可以自己實現一個)
                  local A = Bird:new()
                  LoggerUtils:debug("------------------------------原始值-----------------------------------");
                  LoggerUtils:debug("Bird canFly:" .. StringUtils.boolean2string(A.canFly));
                  LoggerUtils:debug("Bird color:");
                  CommonUtils.printTable(Bird.color)
                  LoggerUtils:debug("a canFly:" .. StringUtils.boolean2string(A.canFly));
                  LoggerUtils:debug("a color:");
                  CommonUtils.printTable(A.color)
                  --改變A的屬性
                  A.canFly = false
                  A.color[1] = "red"
                  A.color[2] = "blue"
                  A.color[3] = "green"
                  LoggerUtils:debug("------------------------------A改變后----------------------------------");
                  LoggerUtils:debug("Bird canFly:" .. StringUtils.boolean2string(Bird.canFly));
                  LoggerUtils:debug("Bird color:");
                  CommonUtils.printTable(Bird.color)
                  LoggerUtils:debug("A canFly after change:" .. StringUtils.boolean2string(A.canFly));
                  LoggerUtils:debug("A color after chagne:");
                  CommonUtils.printTable(A.color)
                  LoggerUtils:debug("-------------------------------B的值----------------------------------");
                  local B = Bird:new()
                  LoggerUtils:debug("B canFly:" .. StringUtils.boolean2string(B.canFly));
                  LoggerUtils:debug("B color:");
                  CommonUtils.printTable(B.color)

          代碼執行結果:
          2014-12-29 11:20:40,690 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: ------------------------------原始值-----------------------------------
          2014-12-29 11:20:40,690 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: Bird canFly:true
          2014-12-29 11:20:40,691 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: Bird color:
          2014-12-29 11:20:40,691 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: a canFly:true
          2014-12-29 11:20:40,691 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: a color:
          2014-12-29 11:20:40,691 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: ------------------------------A改變后----------------------------------
          2014-12-29 11:20:40,691 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: Bird canFly:true
          2014-12-29 11:20:40,691 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: Bird color:
          2014-12-29 11:20:40,692 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: 1:red
          2014-12-29 11:20:40,692 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: 2:blue
          2014-12-29 11:20:40,692 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: 3:green
          2014-12-29 11:20:40,692 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: A canFly after change:false
          2014-12-29 11:20:40,692 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: A color after chagne:
          2014-12-29 11:20:40,693 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: 1:red
          2014-12-29 11:20:40,693 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: 2:blue
          2014-12-29 11:20:40,695 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: 3:green
          2014-12-29 11:20:40,695 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: -------------------------------B的值----------------------------------
          2014-12-29 11:20:40,695 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: B canFly:true
          2014-12-29 11:20:40,695 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: B color:
          2014-12-29 11:20:40,695 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: 1:red
          2014-12-29 11:20:40,695 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: 2:blue
          2014-12-29 11:20:40,696 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: 3:green
          發現神馬問題了嗎?
          當A的類型為表的屬性color改變時,原始類的color屬性也改變了,同時這個改變也影響到新建的B,而類型為基本類型的屬性canFly就沒有這個問題。
          我的解決方法是新增一個set方法:
          function Bird:setColor(color)
              self.color = color
          end

          然后修改改變屬性的方式:
          local color ={}
                  color[1] = "red"
                  color[2] = "blue"
                  color[3] = "green"
                  A:setColor(color)
          輸出結果:
          2014-12-29 11:31:58,648 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: ------------------------------原始值-----------------------------------
          2014-12-29 11:31:58,648 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: Bird canFly:true
          2014-12-29 11:31:58,649 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: Bird color:
          2014-12-29 11:31:58,649 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: a canFly:true
          2014-12-29 11:31:58,649 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: a color:
          2014-12-29 11:31:58,649 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: ------------------------------A改變后----------------------------------
          2014-12-29 11:31:58,649 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: Bird canFly:true
          2014-12-29 11:31:58,650 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: Bird color:
          2014-12-29 11:31:58,650 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: A canFly after change:false
          2014-12-29 11:31:58,650 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: A color after chagne:
          2014-12-29 11:31:58,650 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: 1:red
          2014-12-29 11:31:58,650 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: 2:blue
          2014-12-29 11:31:58,650 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: 3:green
          2014-12-29 11:31:58,651 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: -------------------------------B的值----------------------------------
          2014-12-29 11:31:58,651 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: B canFly:true
          2014-12-29 11:31:58,653 [main] DEBUG server.app.game.util.LoggerUtils:34 - LUA: B color:
          另外同事一個解決方法更簡單,直接修改new()方法,其它的地方都不用改:
          function Bird:new(o)
              o = o or {}
              setmetatable(o, self)
              self.__index = self
              o.color = {} 
              return o
          end
          這個問題基本上網上的示例貌似都沒提到,我讀的書里也沒有,實際調試過程中才發現的,會造成新創建的類里會有不該有的屬性,比較蛋疼。
          具體原因不了解,有木有筒子指教一下?順便問問這兩種方法哪種更好?
          posted on 2014-12-29 11:42 Jimi 閱讀(10777) 評論(5)  編輯  收藏 所屬分類: LUA

          FeedBack:
          # re: LUA 面向對象編程中的一個坑
          2014-12-29 16:53 | 威客
          很厲害博主  回復  更多評論
            
          # re: LUA 面向對象編程中的一個坑
          2015-01-04 15:47 | flyforlove
          你確定你寫的測試程序沒問題?  回復  更多評論
            
          # re: LUA 面向對象編程中的一個坑
          2015-01-05 09:39 | Jimi
          @flyforlove
          有問題麻煩請指出來  回復  更多評論
            
          # re: LUA 面向對象編程中的一個坑
          2015-01-30 18:27 | poetao
          --改變A的屬性
          A.canFly = false
          A.color[1] = "red"
          A.color[2] = "blue"
          A.color[3] = "green"
          ===改成如下就可以了===============
          A.canFly = false
          A.color = { "red", "blue", "green" }

          ===建議你詳細看下元表的__index實現  回復  更多評論
            
          # re: LUA 面向對象編程中的一個坑
          2015-03-07 11:15 | Jimi
          @poetao
          多謝指教  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 五华县| 隆回县| 福泉市| 尼木县| 山阳县| 绥芬河市| 长垣县| 新竹县| 张家口市| 克什克腾旗| 阜新| 眉山市| 马鞍山市| 利辛县| 康平县| 达日县| 许昌市| 天气| 南川市| 威海市| 泽库县| 铜梁县| 固安县| 潼南县| 大化| 长春市| 桐城市| 田阳县| 顺平县| 和林格尔县| 清河县| 乌拉特中旗| 镇远县| 大渡口区| 海淀区| 汽车| 保亭| 隆安县| 惠东县| 新安县| 璧山县|