在类中this 的使用
this 当前对象
如何确定当前对象:那个对象调用方法,this就是那个对象
//通过方法定义小狗的行为
public void eat(){
System.out.println(this.name+ "小狗再吃");
this.showInfo();
}
//让二个小狗分别调用吃的方法
dog1.eat();
dog2.eat();
方法中的this就分别代表了dog1和dog2
使用this 可以调用类中的属性和方法
public void eat(){
System.out.println(this.name+ "小狗再吃");
this.showInfo();
}
使用this可以调用当前类的构造函数
使用this() 表示调用本类的另一个构造方法,该命令只能出现在构造方法的第一句话中