Created
August 14, 2008 05:11
-
-
Save speedmax/5372 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- global | |
people = {} | |
person = class { | |
function(self, name, age) | |
self.name, self.age = name, age | |
table.insert(people, self) | |
end | |
-- generic by default | |
get_name = function(self) | |
return self.name | |
end | |
-- class method | |
['class.find_by_name'] = function(name) | |
table.foreach(people, function(p) | |
if p.name:match(name) then | |
return p | |
end | |
end) | |
end | |
} | |
p1 = person('taylor luk', 26) | |
p2 = person('minhee', 99) | |
p3 = person('lee', 100) | |
print(p1.get_name()) -- prints 'taylor luk' | |
print(person.get_name(p2)) -- prints 'minhee' | |
-- class method example | |
print person.find_by_name('taylor').name -- prints 'taylor luk' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment