本教程操作环境:windows7系统、java10版,DELL G3电脑。
1.用途
this.属性 = 属性值 表示赋值给当前对象属性值
this.方法() 表示调用当前对象的方法
this() 调用构造方法,在其他构造方法中调用时需要放在第一行
this作为方法参数,将当前对象作为方法参数传递
this作为返回值,将当前对象作为返回值返回
2.注意事项
(1)this() 不能使用在普通方法中,只能写在构造方法中
(2)必须是构造方法中的第一条语句
3.实例
this用于调用构造方法
publicclassConstract{
inti=10;
//static方法不能访问实例变量
publicstaticvoidmain(String[]args){
Dated=newDate();
d.show();
Dated2=newDate(2019,4,7);
d2.show();
}
}
publicclassDate{
privateintyear;
privateintmonth;
privateintday;
Date(){
this(1970,1,1);
}
Date(intyear,intmonth,intday){
this.year=year;
this.month=month;
this.day=day;
}
publicvoidshow(){
System.out.println(this.year+"年"+this.month+"月"+this.day+"日");
}
}
原文来自:https://www.py.cn
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END



















































暂无评论内容