2 ms·
> The difference here is that inheritance is more like a node graph and a not top-down hierarchy, where the only real root object is the super-generic "object"
by silentbicycle 18y ago
> The difference here is that inheritance is more like a node graph and a not top-down hierarchy, where the only real root object is the super-generic "object" object.
Right, although there isn't necessarily an Object that everything inherits from.
You have an object, whose properties (both value fields and methods) are contained in a string-keyed table. If you want to inherit from it, you create another (probably via a method called "new") which defers to it when anything tries to find keys the new object hasn't overridden.
If you think of the initial object as a Class object that instantiates instances, that's the idea, but you don't need to have an explicit class for something you'll only ever have one instance of - you can just build it in place. (Want a singleton? Don't give it a method that spawns new instances. Done.) It's more generalized than OO systems in which Classes and instances are two fundamentally different things.
It may be helpful to read the chapter on OO in _Programming in Lua_ (linked above): It has a very concise intro to prototype-based OO. Lua may also be worth a look since you're interested in game development -- it is quite popular as an embedded scripting language in the game industry (and a pretty cool language in general, I think :) ).
Also, see: http://c2.com/cgi/wiki?ClassesPrototypesComparison http://c2.com/cgi/wiki?ClassesPrototypesComparison