这与(这)之间的区别是什么?

huangapple go评论119阅读模式
英文:

What is the difference between this and (this)?

问题

public class classA implements interfaceA {

  @Override
  public Optional<T> methodA(String s) {
    return this.methodA(s);  // &lt;-  IDEA标记为递归
  }

  @Override
  public Optional<T> methodB(String s) {
    return this.methodB(s); // &lt;- IDEA标记为递归
  }

我还没有看到这种形式中的(this)。有人可以链接到文档或解释常规形式和括号中的形式之间有什么区别吗?
英文:
public class classA implements interfaceA {

  @Override
  public Optional&lt;T&gt; methodA(String s) {
    return (this).methodA(s); // &lt;-  IDEA doesn&#39;t underlines as recursion
  }

  @Override
  public Optional&lt;T&gt; methodB(String s) {
    return (this).methodB(s); // &lt;- IDEA doesn&#39;t underlines as recursion
  }
public class classA implements interfaceA {

  @Override
  public Optional&lt;T&gt; methodA(String s) {
    return this.methodA(s);  // &lt;-  IDEA underlines as recursion
  }

  @Override
  public Optional&lt;T&gt; methodB(String s) {
    return this.methodB(s); // &lt;- IDEA underlines as recursion
  }

I have't seen (this) in such a form. Can anyone link reference to documentation or explain what is the difference between usual form and in parentheses?

答案1

得分: 9

在你的示例中完全没有区别。那些花括号在你的示例中是不需要的,应该移除。

显然,这也会触发 IntelliJ 的递归检测。这只是该功能中的一个小错误,不表示代码的运行方式有任何不同。

英文:

There's no difference whatsoever. Those braces are not needed in your example and should be removed.

Apparently it also trips up IntelliJ's recursion detection. That's just a minor bug in that feature, not an indication that the code works differently.

答案2

得分: 1

语言规范中:

一个括号表达式是一个主要表达式,其类型是包含表达式的类型,运行时的值是包含表达式的值。如果包含表达式表示一个变量,那么括号表达式也表示该变量。

由于this是包含的表达式,(this)具有与this相同的类型和值。

然而,this不是一个变量,因此最后部分不适用(您不能写(this) = ...this = ...,但您可以写(a) = ...作为a = ...的等价写法)。

英文:

From the language specification:

> A parenthesized expression is a primary expression whose type is the type of the contained expression and whose value at run time is the value of the contained expression. If the contained expression denotes a variable then the parenthesized expression also denotes that variable.

Since this is the contained expression, (this) has the same type and value as this.

However, this is not a variable, so the last part does not apply (you can't write (this) = ... or this = ..., but you can write (a) = ... as an equivalent to a = ...).

答案3

得分: -1

这两者之间没有区别。

英文:

There is no difference between these two.

huangapple
  • 本文由 发表于 2023年8月10日 21:28:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定