Overiding methods help needed

BlitzMax Forums/BlitzMax Beginners Area/Overiding methods help needed

QuickSilva(Posted 2008) [#1]
Can anyone please provide me with a very simple example of how you overide methods and the benefits of doing so as I am having lots of problems trying to figure it out.

Any advice would be great.

Jason.


Dreamora(Posted 2008) [#2]
You can not overwrite methods other than redefine them with the exactly same type header.

so if the original was:

Update(timedif:float)

then the one on the extended type must be

Update(xxx:float) as well

you can not remove that nor add something else. nor can you define 2 different versions of update.


Czar Flavius(Posted 2008) [#3]
I'm not sure if this is a good way to do it but I just had this idea of a way in which you could use overridden methods.

You have a base vehicle type which is abstract. That means you cannot make an actual vehicle - it is a template.

A vehicle variable can accept any type that extends from it. Here we have smallship and largeship. Each ship has a different upgrade method, and you would put any actions unique to a type of ship in its own type. Only common actions are placed in vehicle.

Type vehicle Abstract
   Method upgrade:vehicle() Abstract
   Method move()
      'assuming all vehicles move in the same way put common code in this type
   End Method
End Type

Type smallship Extends vehicle
   Method upgrade:vehicle()
      Local newvehicle:vehicle = New largeship
      Return newvehicle
   End Method
End Type

Type largeship Extends vehicle
   Method upgrade:vehicle()
      Local newvehicle:vehicle = New nextship
      Return newvehicle
   End Method
End Type


'''''''''

Local player:vehicle = New smallship
player = player.upgrade() 'now a bigship