【发布时间】:2019-01-15 17:01:59
【问题描述】:
我最近读到this 是一个局部变量,它包含当前对象的引用 ID,可以在任何实例函数中使用。但是,当我将 this 明确声明为 int 参数时,我收到编译时错误消息:“接收器类型与封闭类类型不匹配”。
class ThisDemo
{
void show(int this)
{
System.out.println(this);
}
}
class ThisDemo1
{
public static void main(String... s)
{
ThisDemo a=new ThisDemo();
int x=10;
a.show(x);
}
}
【问题讨论】:
-
请注意,声明一个名为
this的(接收者)参数实际上是有效的。它必须是正确的类型。 ideone.com/WTGOV1