JAVA this

this引用:在非static方法和构造器中可以使用。在方法中this代表操作的是本对象,构造器中表示正在初始化的对象,用法:

public Apple
{
	String name;
	public Apple(String name)
	{
		this.name = name;     //如写成name=name不能达到目的,this.name代表正在初始化的对象的name field。
	}
}

this调用:只能在构造器的第一行使用。调用方法中其他的构造器:

public test(int a,int b)
{
	//构造器内容
}
public test(int a,int b,int c)
{
	this(a,b);    //this调用上一个构造器
}
posted @ 2019-02-28 23:45  epiphanyy  阅读(8)  评论(0)    收藏  举报  来源