|
Lets jump right in with the most simple example, which you can either copy or write yourself as long as
you do actually work through it!
//***************************************************************************
//**** Ex 1 - Simple example of Object Orientation in Jamagic **************
//***************************************************************************
winMain = New Window(640,480);
// Lets make an object!
CurWindow.WriteLn("About to create object...");
myObject = New SimpleObject();
CurWindow.WriteLn("Created object!");
While(TRUE);
//***************************************************************************
//**** Below is all the code for a user object - SimpleObject ***************
//***************************************************************************
Function SimpleObject()
{
//*** This is our first object called - SimpleObject.
//*** This function runs whenever we create a new SimpleObject
//*** This is sometimes called the "constructor".
//*** Currently its very basic!
CurWindow.WriteLn("Creating object...");
}
//***************************************************************************
//**** End of Code for object - SimpleObject ****************************
//***************************************************************************
This creates a useless object, it does not even show up in the debugger! (it does exist thought because you can
delete the object without Jamagic complaining about variable not found) But technically we did create an object.
The basic structure is a function defining how to create the object (the function 'SimpleObject' ).
This code just tells Jamagic about the object and is often called the "Constructor", it is not run until Jamagic reaches the line:
myObject = New SimpleObject();
At which point Jamagic creates an object of type "SimpleObject". This is also sometimes called "making an instance of [OBJECT_NAME]". In general we can have as many "SimpleObjects"
as we want. This is where the myObject part comes in. It is the same as variable names and has the same
rules (no spaces in the name, not allowed to start with a number etc).
'myObject' is a pointer and points at a specific "SimpleObject" in memory. If we have lots of "SimpleObject's" then we would give them
all unique names so that when we want to use a certain one we have a way to tell it apart from all the others.
Do not worry if this does not make sense yet, all will become clear as we move on hopefully as we see some more realisitc examples.
» Next : 3. Member Variables
|