java - 为什么我们可以使用 'this' 作为实例方法参数?

标签 java parameters java-8 this

什么是 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/24291091/

相关文章:

java - 使用java在mysql数据库中删除表

java - 从 Tomcat 服务器迁移到 JBoss 时数据源错误?

java - SWT CLabel 垂直颜色渐变

java - 如何在android中每1分钟移动一次获得不同的纬度和经度?

C++ 更新函数参数

linux - Bourne Shell - 如何识别第一个参数是 ''

javascript - “dbclick”事件监听器不工作,但 'click' 正常

java - 将 int 数组分解为流

java - 如何将 WatchService 更改为流 api

java - 自 Java 8 以来是否有使用 getter 和 setter 的新论点?