[previous] [up] [next]     [contents] [index]
Next: Overriding Methods Up: MrEd Applications: An Example Previous: Using Threads

Defining a Class

Figure ``Class'' shows a class definition that encapsulates a phone book search session. The class syntactic form creates a new class. The first part of a class expression specifies a base class for the new class. In the the example, null (the empty list) is used as the base class, indicating that there is no base class pb-session%. The body of a class expressions defines the instance variables of the new class. Keywords such as public and private are used to declare new instance variables.

(define pb-session%
  (class null ()
    (public
      [refresh-number-info ...])
    (public
      (a-frame (make-object mred:frame% null "Phonebook"))
      (a-panel (make-object mred:vertical-panel% a-frame)))
    (private
      (h-panel (make-object mred:horizontal-panel% a-panel))
      (name-text (make-object mred:text% h-panel
                              (lambda (self event) (refresh-number-info))
                              "Name" ""))
      (quit-button (make-object mred:button% h-panel
                                (lambda (self event) (send a-frame show #f))
                                "Quit"))
      (number-selector (make-object mred:radio-box% a-panel
                                    (lambda (self event) (refresh-number-info))
                                    "" -1 -1 -1 -1 (list "Home Number" "Office Number")))
      (number-text (make-object mred:text% a-panel
                                (lambda (self event) #f)
                                "Number" "(Unknown)")))
    (sequence
      (send a-frame show #t)
      (send number-text set-editable #f))))
-- Figure: Class definition for a phone book session --

The pb-session% class has several public instance variables: the procedure refresh-number-info, and the graphical elements a-frame and a-panel. The instance variables for the control graphical elements are private.

An instance of pb-session% is created using make-object:

  (define a-session (make-object pb-session%))

Since the initialization procedure for pb-session% takes no arguments, no intialization arguments are specified in this make-object expression. Evaluating this expression creates a phone book search frame, just as in the original implementation using global variables. The only difference is that multiple independent frames can be opened by making multiple instances of pb-session%.

The public instance variables of a-session can be accessed with the ivar form. The expression (ivar a-session a-frame) returns the frame object of a-session. Private variables cannot be obtained with ivar. Neither public nor private variables can be changed from ``outside'' the object.

The instance variable refresh-number-info has a procedure value. This procedure can be called directly to force a phone number search in a-session:

  ((ivar a-session refresh-number-info))
This expression extracts the procedure value of the instance variable and then invokes the procedure. When an instance variable has a procedure value, then the instance variable is a method. Therefore, the send syntactic form can be used with refresh-number-info:
  (send a-session refresh-number-info)
This expression is equivalent to the previous one.


[previous] [up] [next]     [contents] [index]
Next: Overriding Methods Up: MrEd Applications: An Example Previous: Using Threads

PLT