1:this在构造方法中:this可以进行构造方法中的相互调用,this(参数);

2:this调用方法中,代表调用该方法的对象的地址,例如下面的代码比较

 package thisTest;

 public class Student {

     public static void main(String[] args) {
Student student = new Student();
System.out.println(student);
student.run();
}
public void run() {
System.out.println(this);//this代表当前调用这个方法的对象的地址
}
}

输出结果是:thisTest.Student@52e922
      thisTest.Student@52e922   所以可以明显看到this和创建的student是相同的东西,都代表创建对象所在栈的地址。

还有一个就是,在另外一个方法中调用另一个方法,我们知道直接可以进行方法名(实参列表)来调用,其实实际上是this.方法名(实参列表)  理解:this代表调用这个方法的一个实例对象,那么this.方法名(实参列表)就是对象调用了一个方法,很容易理解啦,但是和构造方法不同的是:方法直接的调用是this.   来相互调用的,所用this可以 省略,但是在构造方法中是用this(参数列表)来进行多个构造方法中的相互调用,所以this无法省略,更重要的区别是:构造方法this只能用一次,因为只能放在有效代码的首句,首句只有一个,所以就只能来一次,而普通方法中的可以N多次。

3:this调用属性:在一个方法中如果存在了一个和类中的成员变量同名的局部变量,这个时候如果要用全局变量的值则需要用this来调用,原因:局部变量会对全局变量进行覆盖,this代表调用这个方法时的当前实例对象,那么this.name就是全局变量了,很好理解吧。

注意:::::::!!!!this不能使用在静态方法中,原因:this是代表实例对象,static修饰的静态方法是类加载时就分配了方法的入口地址,

所以不可能在加载时有实例对象的

下面有几个考察this的代码,试着区分一下, 检查自己的理解成果:

 public class Student {

     String name;

     void doHomework(){
System.out.println(this.name+"正在做作业.......");
} void speak(){
new Student().doHomework();
System.out.println(name+"正在说话......");
} public static void main(String[] args) {
Student student = new Student();
student.speak();
}
}
 public class Student {

     String name;

     void doHomework(){
System.out.println(this.name+"正在做作业.......");
} public static void main(String[] args) {
Student student = new Student();
student.name="王五";
student.doHomework();
}
}
 public class Student {

     String name;

     void doHomework(){
System.out.println(this.name+"正在做作业.......");
name="刘颖";
} void speak(){
System.out.println(name+"正在说话......");
} public static void main(String[] args) {
Student student = new Student();
student.doHomework();
student.speak();
}
}
 public class Student {

     int age;
String name; public Student(int age) {
this.age = age;
}
public Student(String name) {
this.name = name;
}
public Student(int age, String name) {
this(age);
new Student(name);
}
public static void main(String[] args) {
new Student(12, "王五");
}
}
 public class Student {

     int age;
String name; public Student(int age) {
this.age = age;
}
public Student(String name) {
this.name = name;
}
public Student(int age, String name) {
this(age);
new Student(name);
}
public static void main(String[] args) {
new Student(12, "王五");
}
}
 public class Student {

     String name;

     public static void print() {
System.out.println(this.name);
}
public static void main(String[] args) {
print();
}
}

最新文章

  1. cpp 调用python
  2. Lamp和Lnmp环境搭建
  3. jquery实现文件异步上传
  4. 头文件的宏定义#ifndef测试
  5. MVC URL处理
  6. JAVA_HOME 的设置
  7. Struts2基础学习(七)—值栈和OGNL
  8. 201521123037 《Java程序设计》第7周学习总结
  9. [最短路]P1828 香甜的黄油 Sweet Butter
  10. Ubuntu下Maven配置与Maven项目创建教程
  11. tornado httpserver
  12. python 获取mac地址zz
  13. 1. Linux系统介绍
  14. Linux系统在信息社会的发展
  15. CentOS上用Squid搭建HTTP代理小结
  16. git 查看远程分支最后一次提交时间
  17. 源码安装nginx 出现fatal error: pcre.h: 没有此文件或者目录
  18. Java学习05 (第一遍) - JSP与Servlet
  19. 官方教程:Apache Kylin和Superset集成,使用开源组件,完美打造OLAP系统
  20. 题目1102:最小面积子矩阵(暴力求解&最大连续子序列)

热门文章

  1. 修改mp3图片和信息——BesMp3Editor
  2. 安装了Node.js 从VScode 使用node -v 和 npm -v等命令却无效
  3. nginx的请求限制
  4. Linux内存简单汇总
  5. SOAP1.1&SOAP1.2
  6. WDS部署Windows server2012初试
  7. js 闭包解决方案
  8. C# 线程安全集合类
  9. SecureCRT配置华为S5700交换机
  10. office web apps安装部署,配置https,负载均衡(四)安装office web apps相关软件