lua-users home
lua-l archive

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


Jay Mithani wrote:

> Using 'self' over 'Display' when defining the functions for the Display class should fix
> your issue.

Oh, you are right. I didn't expected this. I though `self' would make it
an instance variable. Now I have to rephrase my question: Why is
`self.count' a class variable in the following example while `self.text'
is a member variable?



Display = {}
Display.count = 0

function Display:new ()
   local me = {}
   setmetatable(me, self)
   self.__index = self
   self.count = self.count + 1
   return me
end

function Display:getCount ()
   return self.count
end

function Display:setText (text)
   self.text = text
end

function Display:getText ()
   return self.text
end

dis1 = Display:new()
dis2 = Display:new()
print(dis1:getCount(), dis2:getCount())

dis1:setText('foo')
dis2:setText('bar')
print(dis1:getText(), dis2:getText())