Earlier in this article, I discussed a candle as an object. The candle had certain properties such as its color, length, width, etc. However, how is a candle created?
A candle is created by pouring molten wax into a mold (with a wick inside, of course). When the wax cools, you open the mold and out pops the candle. Using a mold, you can create many candles all with the same characteristics.
A class is, in effect, a mold. All objects are created from classes. When a class is defined, you specify what the properties are (color, height, width, position, etc.) and what the methods are. Objects are then created by instantiating them (a fancy word for pouring the wax into the mold and waiting for it to cool) from the class.
Let's take a quick look at how we can define a class. For the purposes of this example, I will return to the light switch example:
DEFINE CLASS light AS custom status = "OFF" PROCEDURE LightSwitch IF this.status = "OFF" this.status = "ON" ELSE this.status = "OFF" ENDIF ENDPROC ENDDEFINE
This piece of code creates a class called LIGHT. Light has one property, called Status, which is initialized to OFF. The PROCEDURE code is a method which is attached to the class.
In effect, what we have just done, is defined what a "light" object will have and do.
We instantiate, or create, the object from the class using a command or function which says, basically, "create an object with the characteristics of the class and give it a name."
x = CREATEOBJECT("light")
This code, for example, will create an object based on the class called "light" and give the object a name called X. Once we have run this code, we can access all the properties of the object as described before. For example...
? x.Status &&Returns "OFF" x.LightSwitch &&Run method "lightswitch" &&defined in the class ? x.Status &&After running lightswitch, &&this would return "ON"