Defining ClassesTopDefining New Objects

Defining New Objects

We developed a fancier version of Basketball where we drag around an object that looked more like a real basketball.

The structure of an object expression is as follows:

    object {
        def, instance variable, and type declarations

        initialization code
       
        method definitions
    }

Of course you can (and usually do) give it a name using a def declaration:

   def ball: Draggable = object { ... }

Aside from the (long) definition of the object ball, the program is identical to what we had when the ball was simply an orange filled oval.

When we define such an object:

Notice the difference between the move and contains method in ball. Also notice how moveTo cleverly uses the moveBy method of the class via writing self.moveBy(...,...). The self. prefix can be omitted as Grace assumes it is there if you don't provide another receiver for the message.

One subtle point is that the definitions of features body, border, vertical, horizontal, and many more depend on the canvas being available. The canvas is only available in objects that inherit graphicApplication. Thus to make it accessible, we must put the definition of the object inside the definition of the object forming the main program.


Defining ClassesTopDefining New Objects