一。代码
public class textthis {
//定义构造方法
textthis(int a,int b){
this.a=a; //指代上面的 int a;
this.b=b; //指代上面的 int b;
}
//构造方法的重载
textthis(int a, int b, int c){
this(a,b); //使用this引用上面的构造方法 效果同this.a=a; this.b=b; 相同(this调用构造器,必须放到第一句)
this.c=c;
}
//构造方法(方法分为两类)
void sing(){
}
void eat(){
this.sing(); //调用文本中的sing方法
System.out.println("吃饭");
}
public static void main(String args[]){
}
public class textthis {
int a,b,c;
textthis(int a,int b){
this.a=a; //指代上面的 int a;
this.b=b; //指代上面的 int b;
}
//构造方法的重载
textthis(int a, int b, int c){
this(a,b); //使用this引用上面的构造方法 效果同this.a=a; this.b=b; 相同(this调用构造器,必须放到第一句)
this.c=c;
}
//构造方法(方法分为两类)
void sing(){
}
void eat(){
this.sing(); //调用文本中的sing方法
System.out.println("吃饭");
}
public static void main(String args[]){
textthis hi=new textthis(2,3);
//调用构造方法,使用 new 格式为 (方法名 用户定义名=用户定义名 方法名(参数);
textthis hi=new textthis(2,3);
hi.eat();
}
}
二。用法
1.对象的构造方法
分为四点
(1)分配对象空间,并将对象成员变量初始化为0或者为空
(2)执行属性值的显式初始化
(3)执行构造方法
(4)返回对象的地址给相关变量
2.代码的解析(代码上)
3.this(当前对象)不能用于static方法
Static方法是类方法,先于任何的实例(对象)存在。即Static方法在类加载时就已经存在了,但是对象是在创建时才在内存中生成。而this指代的是当前的对象。