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/61784808/

相关文章:

java - 访问数组父级

java - 在 Java 中返回多个值

java - SQLException : No suitable driver found for org. sqlite.JDBC

javascript - 使用URL参数使用JS调用一行文本?

javascript - 在函数中调用函数时处理参数的最佳方式

function - PowerShell 传递函数中接收到的所有参数,并处理带空格的参数

java - 为什么我不能将 lambda 分配给未类型化的谓词引用?但是可以为它分配一个初始化的类型谓词引用吗?

java - Spring Cache - 如何捕获 except 参数中的异常?

java - 选项中的映射无法应用于<方法引用>

Java 默认方法比相同的代码慢,但在抽象类中