• java中this关键字


    一、对象创建的过程

    构造方法是创建Java对象的重要途径,通过new关键字调用构造器时,构造器也确实返回该类的对象,但这个对象并不是完全由构造器负责创建。创建一个对象分为如下四步:

     1. 分配对象空间,并将对象成员变量初始化为0或空

     2. 执行属性值的显示初始化

     3. 执行构造方法

     4. 返回对象的地址给相关的变量

    二、this的本质

    this的本质就是“创建好的对象的地址”! 由于在构造方法调用前,对象已经创建。因此,在构造方法中也可以使用this代表“当前对象” 。

    三、this最常的用法:

    • this.属性:代表本类属性

    • this调用本类方法:代表调用本类方法,如果调用本类的构造方法,this必须位于第一行

    • this:代表当前对象

    • this不能用于static方法中

    四、示例

    一、this代表“当前对象”示例

     1 public class User {
     2     int id;        //id
     3     String name;   //账户名
     4     String pwd;   //密码
     5  
     6     public User() {
     7     }
     8     public User(int id, String name) {
     9         System.out.println("正在初始化已经创建好的对象:"+this);
    10         this.id = id;   //不写this,无法区分局部变量id和成员变量id
    11         this.name = name;
    12     }
    13     public void login(){
    14         System.out.println(this.name+",要登录!");  //不写this效果一样
    15     }  
    16      
    17     public static void main(String[] args) {
    18         User  u3 = new User(101,"高小七");
    19         System.out.println("打印高小七对象:"+u3);
    20         u3.login();
    21     }
    22 }

    运行结果:

    二、this()调用调用本类方法

    类方法分为两种:

    • 普通方法:如果调用本类的普通方法,可以使用“this.方法()”调用

    • 构造方法:如果调用本类的构造方法,可以使用”this(参数…)”调用,必须位于第一行

     1 public class TestThis {
     2     int a, b, c;
     3  
     4     TestThis() {
     5         System.out.println("正要初始化一个Hello对象");
     6     }
     7     TestThis(int a, int b) {
     8         // TestThis(); //这样是无法调用构造方法的!
     9         this(); // 调用无参的构造方法,并且必须位于第一行!
    10         a = a;// 这里都是指的局部变量而不是成员变量
    11 // 这样就区分了成员变量和局部变量. 这种情况占了this使用情况大多数!
    12         this.a = a;
    13         this.b = b;
    14     }
    15     TestThis(int a, int b, int c) {
    16         this(a, b); // 调用带参的构造方法,并且必须位于第一行!
    17         this.c = c;
    18     }
    19  
    20     void sing() {
    21     }
    22     void eat() {
    23         this.sing(); // 调用本类中的sing()方法;
    24         System.out.println("你妈妈喊你回家吃饭!");
    25     }
    26  
    27     public static void main(String[] args) {
    28         TestThis hi = new TestThis(2, 3);
    29         hi.eat();
    30     }
    31 }

    三、返回对象的值:

          代表当前对象

           先了解下当前对象是代表啥意思?当前对象就是指当前正在调用类中方法的对象。

      this关键字除了可以引用变量或者成员方法之外,还可以返回对象的引用,在代码中可以用return this返回当前类的引用

     1 public class ThisTest4 {
     2     public static void main(String[] args) {
     3         ThisDemo4 thisDemo = new ThisDemo4("李四", "杭州人间天堂");
     4         System.out.println(thisDemo);
     5         System.out.println(thisDemo.get());
     6     }
     7 
     8 }
     9 
    10 class ThisDemo4 {
    11 
    12     private String name;
    13 
    14     private String address;
    15 
    16     public ThisDemo4() {
    17 
    18         System.out.println("创建了一个新的ThisDemo4实例......");
    19     }
    20 
    21     public ThisDemo4(String name) {
    22         this();
    23         this.name = name;
    24     }
    25 
    26     public ThisDemo4(String name, String address) {
    27         this(name);
    28         this.address = address;
    29 
    30     }
    31 
    32     public String get() {
    33 
    34         System.out.println("当前对象this:"+this);
    35         return "名字:" + name + ", 地址:" + address;
    36     }
    37 
    38 }

    结果:

    1 创建了一个新的ThisDemo4实例......
    2 com.trc.java.ThisDemo4@8efb846
    3 当前对象this:com.trc.java.ThisDemo4@8efb846
    4 名字:李四, 地址:杭州人间天堂
  • 相关阅读:
    javascript中的预编译问题
    五环
    两列布局
    定位以及z-index
    [vijos1234]口袋的天空<最小生成树>
    [讲解]prim算法<最小生成树>
    [noip模拟]B<构造>
    [JZOJ5343]健美猫<模拟>
    [noip模拟]心<并查集>
    [noip模拟]种花<快速幂+结论>
  • 原文地址:https://www.cnblogs.com/qiaoxin11/p/12540115.html
Copyright © 2020-2023  润新知