6、this关键字
来源:互联网 发布:啵嘴 知乎 编辑:程序博客网 时间:2024/06/06 03:29
一 this关键字
1.1 this关键字
- 在类的方法定义中使用的this关键字代表使用该方法的对象的引用
- 当必须指出当前使用方法的对象是谁时要使用this
- 有时使用this可以处理方法中成员变量和参数重名的情况
- this可以看作是一个变量,它的值是当前对象的引用
如下图所示
this表示对象自己
如下面一段代码
public class Leaf {
int i = 0;
Leaf(int i) {
this.i = i; this.i表示成员变量i,后面的i表示形参i
}
Leaf increament() {
i++;
return this;
}
void print() {
System.out.println("i = " +i);
}
public static void main(String[] args) {
Leaf leaf = new Leaf(100);
leaf.increament().increament().print();
}
}
其内存图如下所示,中间过程省略
阅读全文
0 0
- 6-this、static关键字
- 6、this关键字
- 关键字this
- this关键字
- this关键字
- this 关键字
- this 关键字
- this关键字
- this关键字
- this 关键字
- this 关键字
- this 关键字
- this关键字
- 关键字this
- 关键字this
- this 关键字
- this 关键字
- this关键字
- EasyUI 多条件筛选查询
- 解决WIN7开机点登陆后黑屏很长时间才会进系统,打黑屏补丁无效问题
- Qt 调用打印机参考
- RabbitMQ学习笔记
- 程序员,如何从平庸走向理想?
- 6、this关键字
- 自律者方得自由
- jquery提交带上传文件的form表单
- Flume日志采集
- C++ String类简单实现
- New Document1
- Ubuntu下安装mysql
- eMP开发环境配置说明
- log4j2 升级问题 (包含log4j与slf4j版本)