Properties:
In the last section we were introduced to classes. The classes we created
we very simple and were only used to demonstrate how to create classes and
objects. In this section we will create more useful classes.
Properties of an object represent information about that object. Suppose
you had a class called Car, and one of the properties of the class was
color. Then you could create two car objects: MyCar and YourCar for
example. We could set the color of MyCar to blue and the color of YourCar
to red.
Defining Properties of a class:
Let's create a new class called car as in the above example.
? defineclass "car
Next we create a property of the car called color. To do this we use the
DefineProperty procedure. The DefineProperty procedure takes two
arguments: the name of the class you are adding the property to, and the name
of the property.
? defineproperty "car "color
Let's go ahead and add another property to our car class, called model.
? defineproperty "car "model
Using Properties:
Before we can use the properties we created above, we must create a car object.
Let's create a car object called mycar.
? newcar "mycar
Let's set the color of mycar to blue. To set a property use the object.setproperty
procedure, where object is the name of our object and property is
the name of the property. Since the name of our object is mycar and the
name of our property is color, we will use the mycar.setcolor
procedure.
? mycar.setcolor "blue
Next lets set the model of mycar to Honda. Again we will use the object.setproperty
procedure. Which in this case will be mycar.setmodel
? mycar.setmodel "Honda
Now we have set the properties of our car, let's see how we can get the value of
the properties. To get the value of a property we use the object.property
procedure. If we want to get the color of mycar, we use the mycar.color
procedure.
? show mycar.color
blue
And to get the model of mycar, we use mycar.model
? show mycar.model
Honda
Now lets create another car object called yourcar, and set the color to red and
the model to Mustang.
? newcar "yourcar
? yourcar.setcolor "red
? yourcar.setmodel "Mustang
Now show the properties of yourcar.
? show yourcar.color
red
? show yourcar.model
Mustang
Let's set the color of mycar to the same color as yourcar.
? mycar.setcolor yourcar.color
? show mycar.color
red
Finally let create another car, and set it's color and model randomly.
? newcar "MysteryCar
? MysteryCar.SetColor pick [red green blue yellow black white brown]
? MysteryCar.SetModel pick [Mustang CRV Viper F10 Rabbit Camaro]
Now let's see what our MysteryCar is, yours may be different since it is random.
? show MysteryCar.color
blue
? show MysteryCar.Model
Camaro
In the next section I will discuss Methods.
|