this引用
来源:互联网 发布:关口知宏结婚 编辑:程序博客网 时间:2024/05/16 12:00
this作为一个Java关键字,有两个作用:
n 代表隐含参数的调用
n 调用本类的其它的构造器
关键字this是用来指向当前对象(类实例)的。这里,this.name指的是当前对象的name字段。
例1
public class Person {
private double height = 1.75;
private double weight = 65;
private String name;
public Person(String aName) {
this.name = aName;// 全称应该是:Person.this.name
}
public Person() {
}
}
例2
public class Person {
private double height = 1.75;
private double weight = 65;
private String name;
public Person(String aName) {
this.name = aName;// 全称应该是:Person.this.name
}
public Person(){
this(“zhangsan”);
}
}
例3
class Lamp {
int watts = 60;
boolean isOn = false;//属性声明
Lamp(boolean startOn) {
isOn = startOn;//这里isOn是上面声明的属性
}
public void setIsOn(boolean isOn) {
for (int dummy = 1; dummy < 1000; dummy++) {
System.out.println("The count is " + dummy);
}
this.isOn = isOn;//注意参数和属性名称相同,必须用this关键字来区分不同作用域
}
}
下面的代码用于调用上面的代码
Lamp aLamp = new Lamp();
aLamp.setIsOn(true);
- this引用
- this引用
- this引用
- 对象的this引用
- 3.4 this 引用句柄
- Java "This" reference(引用)
- 关于this引用
- This引用逸出
- this对象的引用
- Java this 引用逃逸
- 对象的this引用
- this 引用逸出
- 内部特殊引用$this
- java中的this引用
- this引用逃逸
- this引用逸出
- 对象的默认引用:this
- 特殊引用$this的使用
- 一种巧妙的取类的虚函数指针的方法
- Android 开发中使用 SQLite 数据库
- 值传递(pass by value)
- 模仿path的列表式导航
- 将二进制数值转换为十进制
- this引用
- CB偶尔代码不自动提示
- Android中查看和管理sqlite数据库
- SpringAOP
- 二进制数值转换成十六进制
- PHP 如何得到某年某月的天数
- Java编码约定
- poj 3277 City Horizon
- 变量初始化