this使用例子

来源:互联网 发布:卸载linux里的软件 编辑:程序博客网 时间:2024/05/21 22:54

import java.io.*;
class Variable{
int x=0,y=0,z=0;//类的成员变量
void init(int x,int y){
 //在类的方法定义中需要引用正在使用该方法的对象时,用this表示
 //this指代对象本身或者访问本类的成员或者调用本类的构造方法
 this.x=x;this.y=y;
 int z=5;//局部变量
 System.out.println("**in init**");
 System.out.println("x="+x+" y="+y+" z="+z);
}
}
public class VariableTest{
 public static void main(String args[]){
  Variable v=new Variable();
  System.out.println("**before init**");
  System.out.println("x="+v.x+" y="+v.y+" z="+v.z);
  v.init(20,30);
  System.out.println("**after init**");
  System.out.println("x="+v.x+" y="+v.y+" z="+v.z);
 }
}

原创粉丝点击