[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: static (class) variables
- From: Nagaev Boris <bnagaev@...>
- Date: Sun, 22 Nov 2015 21:04:40 +0300
On Sun, Nov 22, 2015 at 8:30 PM, Andreas Matthias
<andreas.matthias@gmail.com> wrote:
> 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 = {}
Do not use global variables.
> 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())
>
>
--
Best regards,
Boris Nagaev