Java 中的接收器参数是什么? Java 8 语言规范讨论了this
。
最佳答案
JLS 给出 a hint :
Either way, the receiver parameter exists solely to allow the type of the represented object to be denoted in source code, so that the type may be annotated.
这两种方法是等效的:
class Test {
void m1() { }
void m2(Test this) { }
}
但是后者允许您添加注释:
void m2(@MyAnnotation Test this) { }
//where MyAnnotation can be defined like this for example:
@Target(ElementType.TYPE_USE) @interface MyAnnotation {}
关于java - 为什么我们可以使用 'this'作为实例方法参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61784808/