Public feed #602744

Unnamed

public lua v1 · immutable
Raw Edit
·published 2007-07-04 08:52 UTC
rendered paste body
--Class system test script--Our class function.  This automates creating objects through--the meta table system.  It's really cheating, but we like cheating. ;)--Once the object itself is created, it calls the "__init" method of the--object.  This supports inheritence...in an odd fashion.--This would all of course be rewritten in C or C++ for speed and--introduced or exposed to lua as part of the language.--And yes, I realize the code in this function is insane.function Class(superclass, name)    local cls = superclass and superclass() or {}    cls.__name = name or ""    cls.__super = superclass    cls.__index = cls    return setmetatable(cls, {__call = function (c, ...)        self = setmetatable({__class = cls}, cls)        if cls.__init then            cls.__init(self, ...)        end        return self    end})endPerson = Class()    function Person:__init(name, age)        self.name = name        self.age = age    end        function Person:printName()        print(self.name)    end--Create a new Person objectnewPerson = Person("Bob", 41)newPerson:printName()