this的作用:
1) this是当前对象的一个引用,便于对当前对象参数的使用;
2)可以返回对象的自己这个类的引用,同时还可以在一个构造函数当中调用另一个构造函数
this示例:
public class ThisDemo {
public ThisDemo()
{
this.number=20;
}
public ThisDemo(int number,int number2)
{
this();//调用无参数的构造函数
this.number2=number2;
}
ThisDemo increment(){
number++;
number2++;
return this;
}
private void print(){
System.out.println("number="+number+";number="+number2);
}
public static void main(String[] args) {
ThisDemo tt=new ThisDemo();
tt.increment().increment().increment().print();
}
}以上就是Java关键字之this的示例代码分享的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号