4
我正在學習Lua,我寧願使用冒號(:
)作爲方法。不幸的是,它並不是到處都是。見我的代碼:爲什麼我不能使用Set:union()而不是Set.union?
Set= {} local mt= {} function Set:new(m) local set= {} setmetatable(set,mt) for a,b in pairs (m) do set[b]=true end return set end function Set.union(a,b) local res=Set:new ({}) for k in pairs (a) do res[k]=true end for k in pairs (b) do res[k]=true end return res end mt.__add=Set.union -- why Set:union() is not working here ? s1=Set:new {22,55,77} s2=Set:new {2,5,3} s3=s1+s2
如何使用Set:union()
上提到的地方或者是沒可能在這裏使用?
感謝您的回答!現在我知道爲什麼 – Thomas 2010-09-24 07:51:46
@Thomas,如果這回答了你的問題,你應該將sbk的答案標記爲「接受」 – kikito 2010-10-04 14:48:38