Skip to main content
Engineering LibreTexts

2.03: Defining the Class SBECell

  • Page ID
    36404
  • \( \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}}\)

    As yet there are of course no classes in the new category. However, the main editing pane displays a template to make it easy to create a new class (see Figure 2.2.2).

    This template shows us a Smalltalk expression that sends a message to a class called Object, asking it to create a subclass called NameOfSubClass. The new class has no variables, and should belong to the category SBE-Quinto.

    We simply modify the template to create the class that we really want.

    \(\bigstar\) Modify the class creation template as follows:

    • Replace Object by SimpleSwitchMorph.
    • Replace NameOfSubClass by SBECell.
    • Add mouseAction to the list of instance variables.

    The result should look like Code \(\PageIndex{1}\).

    Code \(\PageIndex{1}\) (Squeak): Defining the Class SBECell

    SimpleSwitchMorph subclass: #SBECell
        instanceVariableNames: 'mouseAction'
        classVariableNames: ''
        poolDictionaries: ''
        category: 'SBE-Quinto'
    

    This new definition consists of a Smalltalk expression that sends a message to the existing class SimpleSwitchMorph, asking it to create a subclass called SBECell. (Actually, since SBECell does not exist yet, we passed as an argument the symbol #SBECell which stands for the name of the class to create.) We also tell it that instances of the new class should have a mouseAction instance variable, which we will use to define what action the cell should take if the mouse should click over it.

    At this point you still have not created anything. Note that the border of the class template pane has changed to red (Figure \(\PageIndex{1}\)). This means that there are unsaved changes. To actually send this message, you must accept it.

    \(\bigstar\) Accept the new class definition.

    The class-creation Template.
    Figure \(\PageIndex{1}\): The class-creation Template.

    Either yellow-click and select accept, or use the shortcut CMD–s (for “save”). The message will be sent to SimpleSwitchMorph, which will cause the new class to be compiled.

    Once the class definition is accepted, the class will be created and appear in the classes pane of the browser (Figure \(\PageIndex{2}\)). The editing pane now shows the class definition, and a small pane below it will remind you to write a few words describing the purpose of the class. This is called a class comment, and it is quite important to write one that will give other programmers a high-level overview of the purpose of this class. Smalltalkers put a very high value on the readability of their code, and detailed comments in methods are unusual: the philosophy is that the code should speak for itself. (If it doesn’t, you should refactor it until it does!) A class comment need not contain a detailed description of the class, but a few words describing its overall purpose are vital if programmers who come after you are to know whether to spend time looking at this class.

    \(\bigstar\) Type a class comment for SBECell and accept it; you can always improve it later.

    The newly-created class SBECell.
    Figure \(\PageIndex{2}\): The newly-created class SBECell.

    This page titled 2.03: Defining the Class SBECell is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Andrew P. Black, Stéphane Ducasse, Oscar Nierstrasz, Damien Pollet via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.