Naming our Animals with Fields and Methods

While our first example for class fields involved naming, we never actually added a name field to our classes. However, rather than just naming our cats, letā€™s also make it tangible by making the name visible in the web browser when we hover over the animal. As we showed in our previous example, fields like the name should be unique to the class instance ā€“ no two cat elements on the page should be showing the same name if they were given different names.

Insert the following code into Cat.jspp:

external $;

module Animals
{
    class Cat
    {
        string name;

        var $element = $(
            """
            <div class="animal">
                <i class="icofont icofont-animal-cat"></i>
            </div>
            """
        );

        void setName(string name) {
            this.name = name;
        }

        void render() {
            $("#content").append($element);
            $element.attr("title", name);
        }
    }
}

We added a new field: ā€˜nameā€™ with type ā€˜stringā€™. We also added a new method ā€˜setNameā€™. It contains only one line of code:

this.name = name;

Youā€™ll notice the method takes one parameter called ā€˜nameā€™:

void setName(string name) {
    this.name = name;
}

In order to disambiguate the ā€˜nameā€™ parameter from our ā€˜nameā€™ class field, we need to use ā€˜thisā€™. When two conflicting names appear in the same method, this is known as ā€œvariable shadowing.ā€ Itā€™s a convenience so that we donā€™t have to come up with different variable names to describe the same concept. Thus, what our statement is saying is: set the ā€˜nameā€™ class field (ā€˜this.nameā€™) to the ā€˜nameā€™ value passed in as an argument to our method.

The ā€˜thisā€™ keyword inside a class refers to the class instance. Class instance methods, like the ā€˜setNameā€™ method we just defined, cannot be called unless the class has been instantiated first. Once weā€™ve instantiated the ā€˜Catā€™ class, we can call the ā€˜setNameā€™ method. When we call the ā€˜setNameā€™ instance method, the ā€˜thisā€™ keyword refers to the class instance that executed the method. Thus, the above statement will set the ā€˜nameā€™ field of the class instance that ā€˜setNameā€™ is being executed on; it is always unique to the class instance; thus, no two class instances will end up having the same ā€˜nameā€™ field set after a ā€˜setNameā€™ method call.

Finally, we added the following statement to our ā€˜renderā€™ method:

$element.attr("title", name);

This will set the HTML ā€˜titleā€™ attribute of our cat HTML element that we render to the page. By setting the ā€˜titleā€™ attribute, we can set the text that appears when we hover over an HTML element. In this case, when we hover over our cat element, we will see its name.

Before we can see our results, we must set the names of our cats in main.jspp. Letā€™s do that:

import Animals;

Cat cat1 = new Cat();
cat1.setName("Kitty");
cat1.render();
Cat cat2 = new Cat();
cat2.setName("Kat");
cat2.render();

Make sure ā€˜setNameā€™ is called before the ā€˜renderā€™ method.

Compile the code. Once again, for Windows, Mac, and Linux users, the command is now all the same:

$ js++ src/ -o build/app.jspp.js

If the program compiled successfully, open index.html. Hover your name over each cat. You should see its name.



JS++ | Fields and Methods

Similar Reads

Creating and Rendering an Animal

Open ā€˜src/Animals/Cat.jsppā€™ and enter the following code:...

Compiling

Compilation is going to be slightly different compared to our previous tutorial examples due to our deep directory structure. Mac/Linux users wonā€™t have to make much of an adjustment, but, for Windows users, we have to start using the Command Prompt....

Styling the Animals

Right now, our cat is quite small and hard to see. Letā€™s modify the styling to make our cat bigger and clearer....

Class Fields

Beyond methods that define the behavior for a class, classes can also hold data of their own. At its core, you can think of classes as data plus methods that operate on that data. For example, with pets like cats, we may want to be able to give the cat a name. The name is the data and examples of methods might be functions that allow us to set or change the name....

Naming our Animals with Fields and Methods

While our first example for class fields involved naming, we never actually added a name field to our classes. However, rather than just naming our cats, letā€™s also make it tangible by making the name visible in the web browser when we hover over the animal. As we showed in our previous example, fields like the name should be unique to the class instance ā€“ no two cat elements on the page should be showing the same name if they were given different names....