英文:
What is the difference between this and (this)?
问题
public class classA implements interfaceA {
@Override
public Optional<T> methodA(String s) {
return this.methodA(s); // <- IDEA标记为递归
}
@Override
public Optional<T> methodB(String s) {
return this.methodB(s); // <- IDEA标记为递归
}
我还没有看到这种形式中的(this)。有人可以链接到文档或解释常规形式和括号中的形式之间有什么区别吗?
英文:
public class classA implements interfaceA {
@Override
public Optional<T> methodA(String s) {
return (this).methodA(s); // <- IDEA doesn't underlines as recursion
}
@Override
public Optional<T> methodB(String s) {
return (this).methodB(s); // <- IDEA doesn't underlines as recursion
}
public class classA implements interfaceA {
@Override
public Optional<T> methodA(String s) {
return this.methodA(s); // <- IDEA underlines as recursion
}
@Override
public Optional<T> methodB(String s) {
return this.methodB(s); // <- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论