"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > The this keyword

The this keyword

Published on 2024-08-14
Browse:913

A palavra-chave this

Definition of this:

  • this is a reference to the current object that invokes a method.
  • Is automatically passed to all methods of a class.

Use of this within methods:

  • Allows you to access members (variables and methods) of the current instance.
  • this is optional when there is no ambiguity, but necessary to differentiate between local and instance variables with the same name.

Example without this:

class Pwr {
    double b;
    int e;
    double val;

    Pwr(double base, int exp) {
        b = base;
        e = exp;
        val = 1;
        if (exp == 0) return;
        for (; exp > 0; exp--) val = val * base;
    }

    double get_pwr() {
        return val;
    }
}

Explicit use of this:

  • Clarifies the code and resolves ambiguity.
  • Example of use to resolve name ambiguity:
class Pwr {
    double b;
    int e;
    double val;

    Pwr(double b, int e) {
        this.b = b;
        this.e = e;
        this.val = 1;
        if (e == 0) return;
        for (; e > 0; e--) this.val = this.val * b;
    }

    double get_pwr() {
        return this.val;
    }
}

When to use this:
Useful for accessing instance variables when names conflict with local variables or parameters.
In constructor methods, to reference the object under construction.

class Pwr {
    double b;
    int e;
    double val;

    Pwr(double base, int exp) {
        this.b = base;
        this.e = exp;
        this.val = 1;
        if (exp == 0) return;
        for (; exp > 0; exp--) this.val = this.val * base;
    }

    double get_pwr() {
        return this.val;
    }
}

class DemoPwr {
    public static void main(String args[]) {
        Pwr x = new Pwr(4.0, 2);
        Pwr y = new Pwr(2.5, 1);
        Pwr z = new Pwr(5.7, 0);
        System.out.println(x.b   " raised to the "   x.e  
            " power is "   x.get_pwr());
        System.out.println(y.b   " raised to the "   y.e  
            " power is "   y.get_pwr());
        System.out.println(z.b   " raised to the "   z.e  
            " power is "   z.get_pwr());
    }
}

Explanation:

  • The Pwr class calculates the power of a number.
  • The use of this is demonstrated to reference instance variables when the method parameters have the same name.
Release Statement This article is reproduced at: https://dev.to/devsjavagirls/a-palavra-chave-this-2gha?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3