this总结

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值