赞
踩
this是JAVA的一个关键字,表示某个对象。this可以出现在实例方法和构造方法中,但不可以出现在类方法中。
this关键字出现在类的构造方法中时,代表使用该构造方法创建的对象。举例:
package this关键字; public class People { int leg,hand; String name; People(String s){ name=s; this.init();//可以省略this,即将“this,init()”;写成“init()” } void init() { leg=2; hand=2; System.out.println(name+"有"+hand+"只手"+leg+"条腿"); } public static void main(String[] args) { People hsy=new People("后绍园");//创建hsy时,构造方法中的this就是对象hsy } }
实例方法只能通过对象来调用,不能用类名来调用。当this关键字出现在实例方法中时,this就代表正在调用该方法的当前对象。
注意:this不能出现在类方法中,这是因为类方法可以通过类名直接调用,这时,可能还没有任何对象诞生。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。