Object Framework for LOGO

Navigation:
Overview
Classes and Objects
Properties
Methods
Using Properties and Methods
Constructors and Destructors
Message Sending
Getters and Setters
Download

Methods:

In the last section we discussed properties which are attributes of an object.  In this section we will look at methods, which are actions that an object can do.  We will give our car class a couple of actions it can perform such as: StartEngine, Drive, and Stop.

Defining Methods of a class:

Before we can modify our car class, we have to delete all instances of the class.  In other words, we have to delete all the car objects before we can modify the car class. We could delete all the cars individually or we can use the following command to delete all the car objects for us.

? foreach carobjects [deletecar ?]

Now we will add the above methods to the car class.  To add a method we use the DefineMethod procedure.  The DefineMethod procedure takes 3 arguments.  The name of the class to add the method to, the name of the method, and the body of the method.  The actual syntax used for the body of the method is the same as the Define primitive in Logo.  You may want to read up on the Define primitive to understand the exact syntax.  But here it is in a nutshell.  The body is made up of a lists, the first list is the arguments for the method, and the rest of the lists are the actual commands to execute. 

Lets write the StartEngine method.  The StartEngine method won't take any arguments, and it will print the message "Engine Started".

? definemethod "car "StartEngine [[] [print [Engine Started]]

Now we will write the Drive method.  This method will take one argument, the speed at which to drive.  The method will print the message "Driving at speed miles per hour", where speed is the value of the argument.

? definemethod "car "Drive [[speed] [print (se [Driving at] :speed [miles per hour])]

Finally let's create the Stop method.  Again this will be a simple method that will just print the message "Car Stopped".

? definemethod "car "Stop [[] [print [Car Stopped]]

Using Methods:

Like properties, before we can use the methods we created above, we must create a car object. Let's create a car object called mycar.

? newcar "mycar

To use a method of an object, we use the object.method procedure. Where object is the name of the object, and method is the name of the method we want to execute. Let's test our StartEngine method with mycar. So we will use the mycar.StartEngine method.

? mycar.StartEngine
Engine Started

Next lets test our Drive method. Remember the Drive method take one argument, the speed at which to drive. 

? mycar.Drive 55
Driving at 55 miles per hour

Finally let's test our Stop method.

? mycar.stop
Car Stopped

In the next section we will see how to use Properties and Methods together