This关键字详细概述

This关键字

什么是this?

	1this是一个变量 ,一个引用。
       this保存的就是当前对象的地址,指向对象本身,即this代表的就是“当前对象”。
    2this存储在堆内存中,存在于对象的内部。
    3this只能用在实例方法中,谁调用这个实例方法,this就是“谁自己”。
    4this不能出现在静态方法中
        	因为this代表当前对象,静态方法中不存在当前对象
        	强行调用会出现错误: 无法从静态上下文中引用非静态 变量 this
    5、大多数情况下,this可以省略 
例子区分实例变量和静态变量
public class Test {
    //定义一个实例变量
    int i = 0;
    //定义一个静态变量
    static int num = 123;
    public static void main(String[] args) {
        //同一个类中能否直接访问变量i?为什么?
        //System.out.println(i);
        //怎样访问i?i是实例变量,实例对象需要通过实例对象去访问
        System.out.println(new Test().i);//匿名对象

        System.out.println("=========================");

        //怎么访问静态变量?类名.变量名
        System.out.println(Test.num);
        //同一个类中能否省略类名?可以
        System.out.println("省略类名——>"+num);
    }
}

image-20220314082009112

image-20220314082436457

image-20220314083046110

this什么时候“不可省略”?

在实例方法中,或者构造方法中,为了区分局部变量和实例变量,
	这种情况下:this. 是不能省略的。
例子
package com.blog;

/**
 * @Author jinhuan
 * @Date 2022/3/14
 */
public class Student {
    /**
     * age
     * */
    Integer age;
    /**
     * name
     * */
    String name;

    /**
     * 无参构造
     * */
    public Student() {
    }

    /**
     * 全参构造
     * */
    public Student(Integer age, String name) {
        this.age = age;
        this.name = name;
    }

    /**
     * Getters and Setters
     * */
    public Integer getAge() {
        return age;
    }

    /*
      分析一下这种写法为什么不好?
           不够见名知意,可读性比较差,我们希望在接收的时候,传来的变量也为age
      怎么解决?
           使用this关键字来区分  同名的实例变量以及成员变量
     
      public void setAge(Integer no) {
       age = no;
      }
     
      如果不加this呢?
           public void setAge(Integer age) {
              age = age;
          }
       那么这两个age都是局部变量(就近原则),和实例变量毫无关联(见例子3证明)
      */
    
    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
例子3(证明就近原则)
Student类
public class Student {
    /**
     * age
     * */
    Integer age;

    /**
     * 无参构造
     * */
    public Student() {
    }

    /**
     * 全参构造
     * */
    public Student( String name) {
        this.age = age;
    }

    /**
     * Getters and Setters
     * */
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        //此处的age不加this
        age = age;
    }
}

Test方法
public class Test {
    public static void main(String[] args) {
        //创建学生对象
        Student student = new Student();
        //给年龄赋值
        student.setAge(18);
        //打印年龄(可见并未赋值成功)
        System.out.println(student.getAge());
    }
}

image-20220314085120717

This使用在构造方法中

1、通过当前的构造方法去调用另一个本类的构造方法,可以使用以下语法格式:
		this(实际参数列表);
			通过一个构造方法1去调用构造方法2,可以做到代码复用。
			但需要注意的是:“构造方法1”和“构造方法2” 都是在同一个类当中。
2this() 这个语法作用是什么?
		代码复用。
3'对于this()的调用只能出现在构造方法的第一行'
例子(构造方法的相互调用)
Student类
package com.blog;

/**
 * @Author jinhuan
 * @Date 2022/3/14
 */
public class Student {
    /**
     * age
     * */
    private Integer age;
    /**
     * name
     * */
   private String name;

    /**
     * 无参构造
     * */
    public Student() {
        //注意构造器中对于this的调用必须存在于第一行!
        this(18,"张三");
        /*this.age = 18;
        this.name = "张三";*/
    }

    /**
     * 全参构造
     * */
    public Student(Integer age, String name) {
        this.age = age;
        this.name = name;
    }

    /**
     * ToString
     * */
    @Override
    public String toString() {
        return "学生信息:{" +
                "姓名 = " + name +
                ", 年龄 = '" + age + '\'' +
                '}';
    }

    /**
     * Getters and Setters
     * */
    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Test方法
public class Test {
    public static void main(String[] args) {
        //调用学生的无参构造方法默认创建的学生对象值为:张三 18岁
        Student student = new Student();
        System.out.println(student);
    }
}

image-20220314091654341

  • 8
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尽欢Sir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值