06: OBP Basics Part III: Methods

"Object behavior and purpose is exhibited through Methods."

Methods are Functions that belong to an object. This ownership is enforced by the use of the Labeling Convention and this pointer. In this lesson you will learn how to define a Method. Also I will introduce to you Constructors and Destructors Methods.

Methods are distinct from standard Functions by applying the following rules:

  1. Label Convention for Methods: Capitalize the first letter of Object, BaseObject, Property, Purpose. Only the Prefix is separated with an underscore(_).

    {Prefix}_{Object}|{BaseObject}{Property}{Purpose}.{ReturnObject}

    Example: Function MyGame_playerWeaponAmmunitionLoad(this.player)


  2. A method that operates on a instance is passed a object of its own as the first parameter using the this pointer.
    Function alienDelete(this.alien)
    	Delete this
    End Function

  3. Methods can only return an object of its own, a base object, and a basic datatype (integer,float,string).

Constructor & Destructors

A Constructor is a Method thats called to create a instance of object. Its purpose is to initialize properties and pointers to the object upon creation.

Function alienNew.alien()
	this.alien=New alien
	aliens=aliens+1 ;global counter
	;default values
	this\id=aliens
	this\x#=20.0
	this\y#=10.0
	Return this
End Function

myalien.alien = alienNew()

A Destructor is a Method thats called to remove a object instance. Its purpose is to remove the object and its pointers from memory upon deletion.

Function alienDelete(this.alien)
	Delete this
End Function

alienDelete(myalien)

With Applied Object-Based Programming, all objects provide a Constructor and Destructor.

Exercise:

  1. Using the Label Convention, Create two Methods for the invader object:
    1. A Method whos purpose is to add a new instance to a Element of the child.invader[] property using a Constructor. Hint: you may have to add another property to the object to keep count of child.invader[] Elements.
    2. A Method whos purpose is to delete instance from a Element of the child.invader[] property using a Destructor.

Email-in this Exercise for Grade!