1.this关键字代表当前对象

  this.属性  操作当前对象的属性

  this.方法  调用当前对象的方法

2.封装对象的属性的时候,经常会使用this关键字

public class Telphone {
private float screen;
private float cpu;
private float mem; public void sendMessage(){
System.out.println("sendMessage");
} public float getScreen() {
return screen;
}
public void setScreen(float screen) {
this.screen = screen; // 操作当前对象的属性
this.sendMessage(); // 调用当前对象的方法
}
public float getCpu() {
return cpu;
}
public void setCpu(float cpu) {
this.cpu = cpu;
}
public float getMem() {
return mem;
}
public void setMem(float mem) {
this.mem = mem;
}
public Telphone(){
System.out.println("com.imooc.Telphone");
}
public Telphone(float newScreen,float newCpu,float newMem){
if(newScreen<3.5f){
System.out.println("...............");
screen = 3.5f;
}
cpu = newCpu;
mem = newMem;
System.out.println("----------------");
}
}

this

this 是自身的一个对象,代表对象本身,可以理解为:指向对象本身的一个指针

this 的用法在 Java 中大体可以分为3种:

1.普通的直接引用

这种就不用讲了,this 相当于是指向当前对象本身。

2.形参与成员名字重名,用 this 来区分:

package s2;

class Person {
private int age = 22;
public Person() {
System.out.println("初始化年龄:" + age);
} public int getAge(int age) {
this.age = age; // 形参与成员名字重名
return this.age;
}
} public class Test03 {
public static void main(String[] args) {
Person Harry = new Person();
System.err.println("Harry's age is:" + Harry.getAge(33));
}
}

运行结果:

初始化年龄:22
Harry's age is:33

3.引用构造函数

这个和 super 放在一起讲,见下面

最新文章

  1. 流程开发Activiti 与SpringMVC整合实例
  2. Android总结之json解析(FastJson Gson 对比)
  3. Unity4.0的使用
  4. 学习Scala01 环境安装
  5. linux查看出口ip 及w3m字符浏览器
  6. 【CodeForces 699B】One Bomb
  7. Linux常用指令---tail | head(查看文件)
  8. jQuery+AJAX实现网页无刷新上传
  9. VS2010
  10. 安装nodejs 后运行 npm 命令无响应处理方法
  11. document对象属性documentMode与CompatMode
  12. React:入门计数器
  13. gulp详细入门
  14. ConcurrentHashMap源码分析(1.8)
  15. 新DevOps八荣八耻
  16. js 翻牌活动效果
  17. 【洛谷P1230】智力大冲浪
  18. Maven安装配置操作
  19. DG备库缺失归档文件GAP日志
  20. Where关键词的用法

热门文章

  1. jqeury 基础
  2. fegin 调用源码分析
  3. 通过Fegin远程调用 ,返回JPA Page 对象报错
  4. 进程与网络监控和ssh简单使用
  5. asp.net string有多行文字
  6. E. Holes(分块)
  7. 你离BAT之间,只差这一套Java面试题
  8. C++语言的url encode 和decode
  9. js之自定义右键菜单
  10. Codeforces 28C Bath Queue 【计数类DP】*