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();
}
}

java中this总结(转载请注明出处)的更多相关文章

  1. Java程序员面试宝典1 ---Java基础部分(该博文为原创,转载请注明出处)

    (该博文为原创,转载请注明出处   http://www.cnblogs.com/luyijoy/  by白手伊凡) 1.    基本概念 1)         Java为解释性语言,运行过程:程序源 ...

  2. 转载请注明出处: https://github.com/qiu-deqing/FE-interview

    转载请注明出处: https://github.com/qiu-deqing/FE-interview Table of Contents generated with DocToc FE-inter ...

  3. 移动端H5页面遇到的问题总结(转载请注明出处)

    最近刚做完一个移动端的项目,产品之无敌,过程之艰辛,我就不多说了,记录下在这个项目中遇到的问题,以防万一,虽然这些可能都是已经被N多前辈解决掉了的问题,也放在这里,算是为自己漫漫前端路铺了一颗小石子儿 ...

  4. java 代码解压7z(带密码)转载请注明出处,谢谢

    <sevenzipjbinding.version>9.20-2.00beta</sevenzipjbinding.version> <dependency> &l ...

  5. 原创centos7安装hadoop2.7(转载请注明出处)

    启用ip vi /etc/sysconfig/network-scripts/ifcfg-ONBOOT=yes 编辑DNS /etc/resolv.conf nameserver 114.114.11 ...

  6. Apache2.4权限配置(原创帖-转载请注明出处)

    ==================说在前面的话================= 1:这次实验使用的php项目是Discuz,Discuz的安装请参照:http://www.cnblogs.com/ ...

  7. LAMP环境搭建 (原创帖,转载请注明出处)

    =============================说在前面的话==========================第一安装Mysql第二安装Apache-httpd第三安装PHP 第四配置PH ...

  8. Discuz的安装 (原创帖,转载请注明出处)

    ========================写在前面的话========================= 1.LAMP环境搭建请查看这篇日志:http://www.cnblogs.com/yic ...

  9. 阿里云服务器SQLSERVER 2019 远程服务器环境搭建【原创】【转载请注明出处】

    之前做过本地服务器SQLSERVER环境搭建.局域网环境SQLSERVER搭建.一直没有尝试自己完成一个云端服务器的环境搭建.今天就根据一个成功的例子给大家分享一下. 一.云端数据库安装与搭建 我的服 ...

随机推荐

  1. C# WebServices 客户端服务端

    一.编写一个WebService 开发环境:VS2012 1.编写webservice阶段 打开VS2012,新建一个空的web应用程序,我这里用的Framework版本是4.5的 新建好web应用程 ...

  2. elasticsearch 单实例安装启动

    elasticsearch 初次启动 下载 elasticsearch-6.3.2.tar.gz 创建目录 /usr/local/elasticsearch/ 解压 tar -zxf elastics ...

  3. thinkpad T480安装WIN7

    本文转载自http://www.dnxtc.net/zixun/zhuangjijiaocheng/2018-12-01/3256.html 防止忘记 特别把资源集中一下 新买的笔记本预装的WIN10 ...

  4. idea出现灰色或者黄色的波浪线如何去除

    1.File--setting--Editor-Inspections-Geneal-Duplicated Code 去除 主要是类中出现太多的重复代码,idea自动提示.

  5. Druid连接池(无框架)

    关于连接池有不少技术可以用,例如c3p0,druid等等,因为druid有监控平台,性能在同类产品中算top0的.所以我采用的事druid连接池. 首先熟悉一个技术,我们要搞明白,为什么要用他, 他能 ...

  6. Get web site source code

    public String getPageSource() { StringBuffer sb = new StringBuffer(); try { // 构建一URL对象 URL url = ne ...

  7. Badge 标记

    出现在按钮.图标旁的数字或状态标记. 基础用法 展示新消息数量. 定义value属性,它接受Number或者String. <el-badge :value="12" cla ...

  8. Docker,用任何工具链和任何语言来构建任何应用

    在看过Docker的两个Hello World的程序后,我们对Docker有了一个大概的感性的认识,那么Docker是到底是什么呢?Docker是一个面向开发者和系统管理员编译,装载,和运行分布式应用 ...

  9. HideTcpip.c

    隐藏tcp端口,来自看雪 /////////////////////////////////////////////////////////////////////////////////////// ...

  10. windows修复失效图标

    taskkill /im explorer.exe /fcd /d %userprofile%\appdata\localdel iconcache.db /astart explorer.exeex ...