Object Framework for LOGO

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

Constructors and Destructors:

In the last section we talked about Method and Properties. In this section we will look at two special methods, the constructor and the destructor. The constructor method is called when an object of a class is created. The destructor is called when an object of a class is deleted.

Constructor:

The constructor is a method of a class that is called to create an object of a class. When you create a class, a constructor is automatically created. So with the classes we have created so far: house, car, and box, the constructors newhouse, newcar, and newbox were automatically created for us.

The constructor that is automatically created is very simple, and simply creates a new instance of the class. However, sometimes you want to perform other actions when an object is created. Let's create a simple constructor for demonstration purposes.

First let's create a new class called Student.

? Defineclass "Student

Our constructor for the Student class will simply print "Student Created" when a new student is created.

To create a constructor for a class, use the DefineConstructor procedure. The DefineConstructor procedure takes 2 arguments: The name of the class, and the constructor body. The constructor body is a series of lists, where the first list are any arguments for the constructor, and the rest of the lists make up the body of the constructor. (The body of the constructor is the same as the Logo Define primitive. Look up Define for further reference.)

? DefineConstructor "Student [[] [print [Student Created]]

Now let's test our constructor by creating a couple of Students.

? newstudent "student1
Student Created
? newstudent "student2
Student Created

The constructor we created wasn't very useful. Most of the time, we use constructors to initialize the properties of a class. Let's extend our student class so I can demonstrate a more useful constructor.

Before we can modify our student class, we must delete all instances (student objects) of the class.

? foreach studentobjects [deletestudent ?]

Let's give the student class three properties called: FirstName, MiddleName and LastName.

? DefineProperty "Student "FirstName
? DefineProperty "Student "MiddleName
? DefineProperty "Student "LastName

Next, we will create a method that outputs the student's full name.

? DefineMethod "Student "FullName [[] [output (se thisStudent.FirstName thisStudent.MiddleName thisStudent.LastName)]]

Finally, we will create a constructor for the student class that takes three parameters: First, Middle, and Last. We will then set the student's FirstName to First, MiddleName to Middle, and LastName to Last. (This will automatically overwrite the constructor we created earlier.)

? DefineConstructor "Student [[First Middle Last] [thisStudent.SetFirstName :First] [thisStudent.SetMiddleName :Middle] [thisStudent.SetLastName :Last]]

Now let's create three students: Billy Joe White, Sally Ann Brown, and Mark Eric Green.

? newStudent "student1 "Billy "Joe "White
? newStudent "student2 "Sally "Ann "Brown
? newStudent "student3 "Mark "Eric "Green

And we will test our FullName method with each student, to ensure everything works correctly.

? print student1.fullname
Billy Joe White
? print student2.fullname
Sally Ann Brown
? print student3.fullname
Mark Eric Green

Destructor:

The destructor is a method of a class that is called when an object is deleted. When you create a class, a destructor is automatically created. So with the classes we have created so far: house, car, box, and our student class above, the destructors deletehouse, deletecar, deletebox, and deletestudent were automatically created for us.

The destructor that is automatically created is very simple, and simply deletes the instance of the class. However, sometimes you want to perform other actions when an object is deleted. Let's create a simple destructor for demonstration purposes.

We will modify the Student class, and add a destructor to it. Before we can modify our student class, we must delete all instances (student objects) of the class.

? foreach studentobjects [deletestudent ?]

Our destructor for the Student class will simply print "Student Deleted" when a student object is deleted. To create a destructor for a class, use the DefineDestructor procedure. The DefineDestructor procedure takes 2 arguments: The name of the class, and the destructor body. The destructor body is a series of lists, where the first list are any arguments for the destructor, and the rest of the lists make up the body of the debstructor. (The body of the destructor is the same as the Logo Define primitive. Look up Define for further reference.)

? definedestructor "Student [[] [print [Student Deleted]]

Finally, let's test the destructor by creating a couple of students and then deleting them.

? newstudent "student1 "John "William "Black
? newstudent "student2 "Melissa "Jennifer "Orange
? deletestudent "student1
Student Deleted
? deletestudent "student2
Student Deleted

In the next section I will introduce Message Sending