Methods and OOP

BlitzMax Forums/BlitzMax Beginners Area/Methods and OOP

Ant(Posted 2005) [#1]
Hi there, I've recently come over from DBPro and wondered if there were any tutorials/introductions to OOP (I'm currently baffled by 'methods' and their purpose). Any help on this would be greatly appreciated!

Thanks


Dreamora(Posted 2005) [#2]
Method / Field: They are per instance so only exist if you have an object instance and use .methodname or .fieldname

Function / Global / Const are per type. They only exist once for a whole type and can not access Field / Method.
They are called using the typename.functionname ...


Ant(Posted 2005) [#3]
I guess my question is why put a method into a type as opposed to having a standard function that does whatever the method does (I am used to procedural languages so I cant see the advantage yet).

Thanks for the help


Nennig(Posted 2005) [#4]
To understand the interest of methods and fields, first you need to understand the interest of Object Oriented Programming over procedural programming.

Let me try to explain:

OOP permit us to divide the problem at hand into entities.
This makes programming simpler as it allows us to think in a similar manner we solve problems in real life.

Indeed, facing a fairly complex problem, humans tend to divide one big problem into several smaller problems because they are easier to solve.
Once all the smaller problems are solved, the big one is as well.

In a game you may have an entity called "Enemy" and another one called "Player".

Enemy and Player are each of them capable of doing things.
For instance an Enemy might be alble to chase the Player and the Player might be able to jump.

The methods are the list of Actions that entities can do.
For example: enemy1.ChasePlayer() or player1.Jump().

The fields represents the "memory" of each object. Indeed , to accomplish actions (i.e. to use its methods) an object needs to know things about himself like its position e.g. player1.posx =12. This necessary knowledge is strored in the fields.

To give birth to objects, you need to create a Type (equivalent of a class in other OOP language such as Java and C#).
A type is really a blueprint. From one single blueprint of a house you can build as many houses as you want. The same apply to objects. If you create a Type "Enemy" we can create so many objects or "instances" of that type,we want to.

If you decide to create two enenies then at the same time (= in the same frame) one can have a position in X = 12 (i.e. enemy1.posx=12) and the second one can have a position in X = 24 (i.e. enemy2.posx=24).

Similarly at this same time (= in the same frame) enemy1 might be executing its ChasePlayer() method and enemy2 might be executing its Evade() method.

Hope this helps you a bit to grasp OOP.


Ant(Posted 2005) [#5]
Ok, I think I see where you are coming from. Thanks again


Nennig(Posted 2005) [#6]
No prob, have fun with Blitzmax. ;-)


tonyg(Posted 2005) [#7]
Good tutorials...
Beginner's Guide
Absolute Newbie OOP


FlameDuck(Posted 2005) [#8]
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>


Dubious Drewski(Posted 2005) [#9]
Be sure to check this link out:

http://java.sun.com/docs/books/tutorial/java/concepts/

It's nicely diagrammed and it's what made me finally
understand OOP.

Ps, are you the Ant from MaxForums? (For all you Blitzers, that's 3D Studio Max, not blitzmax)


Ant(Posted 2005) [#10]
Ok thanks for all the help - and no, this is a different ant!