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++入门经典-例8.6-多重继承的构造顺序

    1:单一继承是先调用基类的构造函数,然后调用派生类的构造函数,但多重继承将如何调用构造函数呢?多重继承中的基类构造函数被调用的顺序以派生表中声明的顺序为准.派生表就是多重继承定义中继承方式后面的内容, ...

  2. 尚学堂requireJs课程---1、作用域回顾

    尚学堂requireJs课程---1.作用域回顾 一.总结 一句话总结: 尚学堂的课程的资料他的官网上面是有的 1.js作用域? ~ js中是函数作用域:局部变量的话要写var关键词 ~ 闭包可以解决 ...

  3. C# async await and state machine

    Async Await and the Generated StateMachine https://www.codeproject.com/Articles/535635/Async-Await-a ...

  4. 一、Vue基础之常用方法

    一.JSON.parse() 与 JSON.stringify() 1.JSON.parse() :是从一个字符串中解析出 json 对象 //定义一个字符串 var data='{"nam ...

  5. CFS理论模型

    参考资料:<调度器笔记>Kevin.Liu <Linux kernel development> <深入Linux内核架构> version: 2.6.32.9 下 ...

  6. iOS杂记-告警清理

    NS_ASSUME_NONNULL_BEGIN @interface Robot : NSObject @property (copy,readonly) NSString *name; - (nul ...

  7. SELECT * 测试

    描述 大家通常禁止在生产环境直接使用select * 已成常识了,也常常在开发规范中就会规定不允许直接使用select *,那么我们为什么不允许使用select * ,在一些什么场景下select * ...

  8. 【TypeScript】学习笔记 把一些需要记的记录一下

    安装typescript: npm install -g typescript 启动typesctipt自动编译: tsc 文件名.ts --watch 函数参数默认值: 1.有默认值参数的,声明在最 ...

  9. MVC模型简介

    MVC模型:是一种架构型的模式,本身不引入新功能,只是帮助我们将开发的结构组织的更加合理,使展示与模型分离.流程控制逻辑.业务逻辑调用与展示逻辑分离. 首先让我们了解下MVC(Model-View-C ...

  10. 来吧,认识一下Bugcrowd社区的开源课程

    what Bugcrowd? 类似于360补天等众测平台. bugcrowd大学 类似于漏洞银行推出的开源教学资源. 使用指南(网页直接谷歌翻译) 研究员researchers:指的就是我们白帽子. ...