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:
- Label Convention for Methods: Capitalize the first letter of Object, BaseObject, Property, Purpose. Only the Prefix is separated with an underscore(_).
Example: Function MyGame_playerWeaponAmmunitionLoad(this.player)
- 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
- 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.
Email-in this Exercise for Grade!