1. 表示本类的属性 this.name = name;
2. 调用本类的普通方法:this.方法();
3. 调用本类构造方法:this();
- this(); 要放在构造方法的行首,不能放在普通方法中。
- 构造方法使用this()相互调用时,至少保留一个构造方法没有调用其他构造方法。
4. this 表示当前对象
class Staff{ private int ID; private String name; private int salary; private String department; Staff(){} Staff(int ID){ this(ID,"Noname",0,"No"); } Staff(int ID, String name){ this(ID,name,1000,"Back"); } Staff(int ID, String name, int salary, String department){ this.ID = ID; this.name = name; this.salary = salary; this.department = department; } String getInfo() { return "ID: "+this.ID+"\nName: "+this.name+"\nSalary: "+this.salary+"\nDepartment: "+this.department; } void print() { System.out.println("当前对象为:"+this); }}public class TestThis { public static void main(String[] args) { // TODO Auto-generated method stub Staff s1 = new Staff(); Staff s2 = new Staff(100); Staff s3 = new Staff(50,"Tom"); Staff s4 = new Staff(111,"Jack",10000,"Finance"); System.out.println(s1.getInfo()); System.out.println(s2.getInfo()); System.out.println(s3.getInfo()); System.out.println(s4.getInfo()); System.out.println(s1); s1.print(); }}