Methods and OOP

BlitzMax Forums/BlitzMax Beginners Area/Methods and OOP

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

There are three things that define an object. Identity, State and Behavior. The identity is in BlitzMAX represented by a unique named reference, for instance
Local myObject:Whatever = New Whatever '<- myObject is this particular objects identity.
Local anotherObject:Whatever = New Whatever '<- Another Whatever object. It's identity is anotherObject.

In BlitzMAX an objects state, is represented by fields. Say your Whatever object had image, xPosition and yPosition Fields. You could then access these like so:
DrawImage myObject.image , myObject.xPosition , myObject.yPosition ' Draws myObject to the backbuffer.
DrawImage anotherObject.image , anotherObject.xPosition , anotherObject.yPosition ' Draws anotherObject to the backbuffer.[/quote]Now the cool trick here is that you can add all these objects to a datastructure (Array, List, Dictionary) and itterate over them all at once:
[code]For Local i:Whatever = EachIn my dataStructure
  DrawImage i.image , i.xPosition , i.yPosition ' Draws all the Whatever objects added to dataStructure, one at a time.
Next

Methods reflect an objects behavior or interface. "The things you can make an object do". using the following method:
Method Draw()
    DrawImage image , xPosition , yPosition ' Draws the Whatever object it was invoked on.
EndMethod
our loop from before is now reduced to:
For Local i:Whatever = EachIn my dataStructure
  i.draw ' Draws all the Whatever objects added to dataStructure, one at a time.
Next
Which is much easier to read and uncerstand. Why is this better than Functions? The answer is polymorphism - but first, learn rule number one. :o>