Defining Classes |
Classes are used when we need more than one object with the same kind of information and operations. Suppose, for example, that I wanted to have a basketball that could start where ever I wanted to on the screen of any application. (E.g., maybe I want two basketballs to lay next to the laundry baskets in this weeks lab.)
To create multiple objects of the same general kind, we can define a class which can be used to generate objects. We can get from an object to a class with only a few small changes:
For example, see the Basketball with a class and compare it to our previous version above. The line:
def ball: Draggable = object {...}
was replaced by
class basketBall.at(startPosn:Point) size (size:Number) on (canvas:DrawingCanvas) -> Draggable {...}
The formal parameters startPosn, size, and canvas are the items that can vary between uses. For example, in the basketballGame object, the line
def ball = basketBall.at(startLocn)size(ballSize)on(canvas)
will create a ball at startLocn (which was earlier defined to be 190 @ 300), with size ballSize (which was set to 40), and on the canvas that was created for basketballGame.
To help you understand the correspondence, we used the same names as in the definition of ball in the earlier version. Make sure you understand the correspondence.
At the end of class, we looked at a class to generate Tshirts: Tshirt
Defining Classes |