gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:33:40 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com