1.解决类成员变量和参数之间的二义性。
public class this_ {
int eggCount;
public void EggCake(int eggCount){
this.eggCount=eggCount;
}
2.同类中实例方法之间互调
public class this_ {
int eggCount;
public void EggCake(int eggCount){
this.eggCount=eggCount;
System.out.println(eggCount);
}
public void eat() {
this.EggCake(3);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
this_ a = new this_();
a.eat();
}
}
3.将当前对象作为参数传递到给另一个方法。
class food{
public void milk(this_ this_a){
System.out.println("将当前对象作为参数传递到给另一个方法");
}
}
public class this_ {
public void push() {
food fd= new food();
fd.milk(this);//this为当前对象传递
}
}
4.return this 一般是在非静态方法返回,当前类的对象。
class food{ public food milk(this_ this_a){ return this; //this就是food类的对象 } }
5.构造器重载的互调,this(参数)必须卸载构造方法的第一行
class food{
public food(String name) {
}
public food(String name,int a) {
this(name);//调用构造函数food(String name)
}
}
6.static 不能和this 一起使用。因为当字节码被加载JVM,static成员已经存在了,但此时对象还没有创建,没有对象就没有this