java面向对象-------this的使用

package java面向对象;

/*
this关键字的使用,this表示当前创建好的对象
*/

public class TestThis {

int a,b,c;
public TestThis(int a,int b){
//1、通过this来区分局部变量和成员变量
this.a=a;
this.b=b;
}
public TestThis(int a,int b,int c){
//2、通过this调用上面的构造方法,必须放在第一句
//3、this不能在static静态方法中使用
this(a,b);
this.c=c;
}
void sing(){

}
void eat(){
this.sing();//调用本类中sing(),可以不写this
System.out.println("哈哈哈");
}
public static void main(String[] args) {
TestThis tt=new TestThis(20,30);
tt.eat();
}
}

猜你喜欢

转载自www.cnblogs.com/zzzao/p/10888885.html