lua-users home
lua-l archive

static (class) variables

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


In the code below I tried to define a static (class) variable `Display.count'.
It seems to work at first. But doesn't work any more once you rename the class.
What's the correct way to get static variables?
Andreas
Display = {}
Display.count = 0
function Display:new ()
 local me = {}
 setmetatable(me, self)
 self.__index = self
 Display.count = Display.count + 1
 return me
end
function Display:getCount ()
 return Display.count
end
dis1 = Display:new()
dis2 = Display:new()
print(dis1:getCount(), dis2:getCount())
-- now let's rename Display
DDD, Display = Display, nil
dis = DDD:new()
print(dis:getCount())

AltStyle によって変換されたページ (->オリジナル) /