最近的工作主要涉及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 閱讀(10774)
評論(5) 編輯 收藏 所屬分類:
LUA