碼農(nóng)往事
          我的代碼為什么這么丑?一定是因為我長的不好看
          posts - 29,comments - 86,trackbacks - 0
          最近的工作主要涉及LUA,這有個坑必須記一下。
          下面是一個LUA面向?qū)ο髮懛ǚ浅3R姷膶懛ā?br />
          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類只是簡單的封裝類,可以自己實現(xiàn)一個)
                  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)

          代碼執(zhí)行結(jié)果:
          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
          發(fā)現(xiàn)神馬問題了嗎?
          當(dāng)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)
          輸出結(jié)果:
          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
          這個問題基本上網(wǎng)上的示例貌似都沒提到,我讀的書里也沒有,實際調(diào)試過程中才發(fā)現(xiàn)的,會造成新創(chuàng)建的類里會有不該有的屬性,比較蛋疼。
          具體原因不了解,有木有筒子指教一下?順便問問這兩種方法哪種更好?
          posted on 2014-12-29 11:42 Jimi 閱讀(10791) 評論(5)  編輯  收藏 所屬分類: LUA

          FeedBack:
          # re: LUA 面向?qū)ο缶幊讨械囊粋€坑
          2014-12-29 16:53 | 威客
          很厲害博主  回復(fù)  更多評論
            
          # re: LUA 面向?qū)ο缶幊讨械囊粋€坑
          2015-01-04 15:47 | flyforlove
          你確定你寫的測試程序沒問題?  回復(fù)  更多評論
            
          # re: LUA 面向?qū)ο缶幊讨械囊粋€坑
          2015-01-05 09:39 | Jimi
          @flyforlove
          有問題麻煩請指出來  回復(fù)  更多評論
            
          # re: LUA 面向?qū)ο缶幊讨械囊粋€坑
          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" }

          ===建議你詳細(xì)看下元表的__index實現(xiàn)  回復(fù)  更多評論
            
          # re: LUA 面向?qū)ο缶幊讨械囊粋€坑
          2015-03-07 11:15 | Jimi
          @poetao
          多謝指教  回復(fù)  更多評論
            

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 通州区| 镇原县| 上虞市| 遵义县| 黎川县| 山阳县| 东乡县| 周至县| 上栗县| 崇州市| 垣曲县| 泰和县| 嘉禾县| 屯门区| 保定市| 沁水县| 克什克腾旗| 甘德县| 荃湾区| 平昌县| 靖边县| 陕西省| 邛崃市| 白城市| 通化市| 故城县| 兰州市| 亳州市| 罗城| 从江县| 龙泉市| 孟连| 横山县| 喀喇沁旗| 邹城市| 岳池县| 焦作市| 策勒县| 阿拉善左旗| 武隆县| 宣威市|