**
JAVA This的用法
**
先写一个要调用的类 都放在名为test的包下面
在这里插入代码片package test;
public class Demo {
String str="这是成员变量";
void fun(String str)
{
System.out.println(str);
System.out.println(this.str);//当局部变量和成员变量重名的时候,在方法中使用this表示成员变量以示区分
this.str = str;
System.out.println(this.str);
}
}
然后再建立一个主类来调用它
package test;
public class This
{
public static void main(String args[])
{
Demo demo=new Demo();
demo.fun("这是局部变量");
}
}
这是我的文件放置
当局部变量和成员变量重名的时候,在方法中使用this表示成员变量以示区分,所以结果为如图所示
//代码修改自博客园,如觉抄袭请私聊