Prev: Browsing classes. Up: Using BLOX. Next: Saving in BLOX.

Creating classes

Now suppose we want to define an Account class. Click on the Object class in the upper left ``Classes'' pane, and choose Add from the Class menu. Edit it so that it reads as follows instead:

Object subclass: #Account
    instanceVariableNames: 'balance'
    classVariableNames: ''
    poolDictionaries: ''
    category: ''!
When you select ``Accept'' from the Edit menu, you'll see Account appear in the ``Classes'' pane, indented under the Object class. (There may be a dialog box or two to click through. As far as I can tell, your answers to these Yes/No questions aren't important.)

Before we start to define methods, we need to first define a method category. To do this, choose Add... from the Protocol menu, and type the category name (such as access). Then, you can write the method body in the lower pane:

balance: aNumber
    balance := aNumber.
    ^ self
After choosing Accept from the Edit menu, the balance: keyword method will appear among instance method for the Account class listed under the access category.

Go ahead and add the unary balance method, also to the access category.

balance
    ^ balance

If you want to create a class method, click ``class'' in the Method Type area. You can then create a category name (such as creation), and then type the method body.

new: aNumber
    ^ self new balance: aNumber
In this case, we're using the new class method inherited from the Object class to create a raw Account, and then we're calling the balance: instance method on that Account object to initialize the balance.

Now we can test our class so far. Go back to the worksheet, and type the following:

mine := Account new: 100           Do it
mine balance                       Print it
The first line uses the new: class method we defined to create an Account object, and the second sends he binary instance method to that object.

Prev: Browsing classes. Up: Using BLOX. Next: Saving in BLOX.