Overriding Constructor?
BlitzMax Forums/BlitzMax Programming/Overriding Constructor?
| ||
Let's say I have several lists. One is an entity list, another is a list of bullets and missiles, another is a list of particles. How can I add new items to a specific list *and only that list* depending on what it is? It seems that I could override a variable in the class definition and have the new() method perform logic on that basis, but I would like to override the new() entirely. Is there a way to do that? |
| ||
People usually create a constructor method. In blitzmax you can do:Local MyItem:MyClass = New MyClass.Init(parameter1, parameter2,...) Where Init is a method that can be overriden. Operators execution order is: First New object is created and inmediatly the Init method is called. |
| ||
Or using a function: [bbcode]Type TSprite Field x#, y# Function Create:TSprite(x#, y#) Local s:TSprite = New TSprite s.x = x s.y = y Return s EndFunction End Type Local mySprite:TSprite = TSprite.Create(100, 100)[/bbcode] |
| ||
To actually stick to the question: I would like to override the new() entirely. Is there a way to do that? Yes. Literally just define a method named "New" and you're done! But it can't take parameters, because Max's overriding rules require any inherited method to keep the same signature. If you want parameters you should do as therevills suggests and use a constructor-convention instead. Unless your code is for other people (who don't read documentation) to use, the difference is purely aesthetic anyway. |
| ||
But let's say your root object has a default constructor, and you want to actually fully override it instead of having your inherited object constructor executed in addition to the root object constructor. It was OK though, I ended up removing the default constructor and just putting new ones in the inherited classes. |
| ||
You can also use the base constructor and inherited constructor at the same time. When you have a base class, and an inherited class, both New() methods are called. First the constructor of the base class is called, then the constructor of the inherited class (or the other way around, not sure about the order). You could set some default data (which would be used by all inherited classes) in the New() method of the base class, while you use the New() method of the inherited class to put the object in the proper list. Type TBase Method New() Print "Base constructor" End Method End Type Type TDerived Extends TBase Method New() Print "Inherited constructor" End Method End Type test:TDerived = New TDerived This code should be able to print both messages (cannot test it right now, I'm at work). You should also see the order in which they are executed. Last edited 2012 |
| ||
I see. That makes very good sense. It's strange that this OO stuff makes so much common sense but can be so counterintuitive at the same time. Thanks! Last edited 2012 |
| ||
I've made a few mistakes in the code posted earlier (which have now been corrected). I've gotten a chance now to test it and fixed the bugs. There was test.TDerived before, it should have been test:TDerived. And the TDerived didn't inherit the TBase, because of the missing "Extends TBase". Last edited 2012 |