关键字this

*    this:代表的是当前对象的引用
* 用来调用其他的构造方法
* 构造方法中可以通过this关键字调用其他的构造方法
* 1.不能调用自身的构造方法
* 2.一次只能调用一个其他的构造方法
* 3.只能放在有效代码的第一行
public class ThisTest {
public static void main(String[] args) {
This t1= new This();
System.out.println(t1.name+"\t"+t1.age);
}
}
class This{
String name;
int age;

public This(){
this("李白",20);

System.out.println("无惨构造");
}
public This(String n){

System.out.println("有参构造");
this .name=n;


}
public This(String n,int a){
System.out.println("两个参数");
this.name=n;
this.age=a;
}

}
posted @ 2020-11-05 21:14  混子的挽歌  阅读(45)  评论(0编辑  收藏  举报