对象的创建过程
- 分配对象空间,并将对象成员变量初始化
- 执行属性值的显式初始化
- 执行构造方法
- 返回对象的地址给相关的变量
this在java中的用法
this的本质:创建好的对象的地址
在构造方法调用钱,对象已经创建了,所以在构造方法中也可以使用this代表**”当前对象“**
用法:
- 在程序发生二义性时,用this来区分局部变量和成员变量。直接用变量是局部变量,而用this. 是当前对象即成员对象。
- 通过this调用构造器
package text;
public class text01 {
int one , two ,three;
text01(int one,int two){
this.one=one;
this.two=two;
}
//构造器的使用
/*
* text01(int one,int two,int three){
this.one=one;
this.two=two;
this.three=three;
}
*/
//使用this来调用构造器
text01(int one,int two,int three){
this(one, two);//构造器的调用必须放到第一句
this.three=three;
}
public static void main(String[] args) {
text01 std=new text01(1,2);
}
}
- this不能用于static方法中,因为静态方法都在类信息中,而this是当前对象的地址,类信息中没有对象