this关键字的使用
来源:互联网 发布:暗黑战神 源码分析 编辑:程序博客网 时间:2024/04/27 22:28
this关键字的使用是希望在方法内部获得对当前对象的引用。this关键字只能在方法内部使用,表示对“调用方法的那个对象”的引用。this的用法和其他对象引用并无不同。但是,若果在方法内部调用同一个类的另一个方法,就不必使用this,直接调用即可。因为在当前方法中this引用会自动应用于同一类中的其他方法。
eg1:this关键字的使用是希望在方法内部获得对当前对象的引用
public class This {
int i = 0;
This increment(){
i++;
System.out.println(this);
return this;
}
void print(){
System.out.println("i="+i);
}
public static void main(String[] args) {
This x = new This();
System.out.println(x);
x.increment().increment().print();
}
}
result:
com.Leaf@2fdb8f3a
com.Leaf@2fdb8f3a
com.Leaf@2fdb8f3a
i=2
1 0
- this关键字的使用
- this 关键字的使用
- this 关键字的使用
- this关键字的使用
- this关键字的使用
- this关键字的使用
- this关键字的使用
- this关键字的使用
- this关键字的使用
- this关键字的使用
- this关键字的使用
- this关键字的使用
- 有关this关键字的使用
- JAVA this关键字的使用
- this 关键字的使用2
- java this关键字的使用
- java关键字this的使用
- this关键字及super关键字的使用
- Snackbar使用及其注意事项
- 七、本地网页
- 如何让一条小丑鱼会骑自行车
- 四大组件基本原理讲解
- 判断是否有网络链接
- this关键字的使用
- 让App进入后台以后长时间在后台运行,并且能正常接收来自前台的消息
- apk更新、下载、安装(四)---DownloadManager ui方式
- windbg 符号文件设置路径
- AngularJS Documents 官方英文文档
- 多线程操作UI控件——DataGridView假死现象
- android 取消AsyncTask继续运行的解决方案
- stm32和esp8266程序资料分析
- Webview和Html5的交互