1,this关键字是类内部当中对自己的一个引用,可以方便类中方法访问自己的属性。
2,可以返回对象的自己这个类的引用,同时还可以在一个构造函数当中调用另一个构造函数。
3,调用本类中的其它构造方法,调用时要放在构造方法的首行。
public class ThisDemo {
int number;
ThisDemo increment(){
number++;
return this;
}
private void print(){
System.out.println("number="+number);
}
public static void main(String[] args) {
ThisDemo tt=new ThisDemo();
tt.increment().increment().increment().print();
}
}
最后输出number=3
public class Student { //定义一个类,类的名字为student。 public Student() { //定义一个方法,名字与类相同故为构造方法 this(“Hello!”); } public Student(String name) { //定义一个带形式参数的构造方法 } }