Definição de this:
- this é uma referência ao objeto atual que invoca um método.
- É passado automaticamente para todos os métodos de uma classe.
Uso de this dentro de métodos:
- Permite acessar membros (variáveis e métodos) da instância atual.
- this é opcional quando não há ambiguidade, mas necessário para diferenciar entre variáveis locais e de instância com o mesmo nome.
Exemplo sem 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;}}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; } }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; } }
Enter fullscreen mode Exit fullscreen mode
Uso explícito de this:
- Clarifica o código e resolve ambiguidade.
- Exemplo de uso para resolver ambiguidade de nomes:
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;}}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; } }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; } }
Enter fullscreen mode Exit fullscreen mode
Quando usar this:
Útil para acessar variáveis de instância quando há conflito de nomes com variáveis locais ou parâmetros.
Em métodos construtores, para referenciar o objeto em construção.
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());}}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()); } }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()); } }
Enter fullscreen mode Exit fullscreen mode
Explicação:
- A classe Pwr calcula a potência de um número.
- O uso de this é demonstrado para referenciar variáveis de instância quando os parâmetros do método têm o mesmo nome.
原文链接:A palavra-chave this
© 版权声明
THE END
暂无评论内容