Skip to main content
Engineering LibreTexts

3.4: Adding Methods to a Class

  • Page ID
    39580
    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Now let’s add some methods to our class. Select the protocol 'no messages' in the protocol pane. You will see a template for method creation in the editing pane. Select the template text, and replace it by the following (Do not forget to compile it):

    initialize
        super initialize.
        self label: ''.
        self borderWidth: 2.
        bounds := 0 @ 0 corner: 16 @ 16.
        offColor := Color paleYellow.
        onColor := Color paleBlue darker.
        self useSquareCorners.
        self turnOff
    

    Note that the characters '' on line 3 are two separate single quotes with nothing between them, not a double quote! '' denotes the empty string. Another way to create an empty string is String new. Do not forget to accept this method definition.

    The initialize.
    Figure \(\PageIndex{1}\): The newly-created method initialize.

    Initialize methods. Notice that the method is called initialize. The name is very significant! By convention, if a class defines a method named initialize, it is called right after the object is created. So, when we execute LOCell new, the message initialize is sent automatically to this newly created object. initialize methods are used to set up the state of objects, typically to set their instance variables; this is exactly what we are doing here.

    Invoking superclass initialization. The first thing that this method does (line 2) is to execute the initialize method of its superclass, SimpleSwitchMorph. The idea here is that any inherited state will be properly initialized by the initialize method of the superclass. It is always a good idea to initialize inherited state by sending super initialize before doing anything else. We don’t know exactly what SimpleSwitchMorph’s initialize method will do (and we don’t care), but it’s a fair bet that it will set up some instance variables to hold reasonable default values. So we had better call it, or we risk starting in an unclean state.

    The rest of the method sets up the state of this object. Sending self label: '', for example, sets the label of this object to the empty string.

    About point and rectangle creation. The expression 0@0 corner: 16@16 probably needs some explanation. 0@0 represents a Point object with x and y coordinates both set to 0. In fact, 0@0 sends the message @ to the number 0 with argument 0. The effect will be that the number 0 will ask the Point class to create a new instance with coordinates (0,0). Now we send this newly created point the message corner: 16@16, which causes it to create a Rectangle with corners 0@0 and 16@16. This newly created rectangle will be assigned to the bounds variable, inherited from the superclass.

    Note that the origin of the Pharo screen is the top left, and the y coordinate increases downwards.

    About the rest. The rest of the method should be self-explanatory. Part of the art of writing good Pharo code is to pick good method names so that the code can be read like a kind of pidgin English. You should be able to imagine the object talking to itself and saying ”Self, use square corners!”, ”Self, turn off!”.

    Notice that there is a little green arrow next to your method (see Figure \(\PageIndex{1}\)). This means the method exists in the superclass and is overridden in your class.


    This page titled 3.4: Adding Methods to a Class is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.