一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第2天,点击查看活动详情
写在前面👀
this 关键字是Java常用的关键字,可用于任何实例方法内指向当前对象,也可指向对其调用当前方法的对象,或者在需要当前类型对象引用时使用。
一、this.属性名🍕
大部分时候,普通方法访问其他方法、成员变量时无须使用 this 前缀,但如果方法里有个局部变量和成员变量同名,但程序又需要在该方法里访问这个被覆盖的成员变量,则必须使用 this 前缀。
例 1
- 创建一个学生类 Student🍔
public class Student {
public String name; // 姓名
public int id; // 学号
public int age; // 年龄
// 创建构造方法,为上面的3个属性赋初始值
public Student(String name, int id, int age) {
this.name = name; // 设置学生姓名
this.id = id; // 设置学生学号
this.age = age; // 设置学生年龄
}
}
在 Student 类的构造方法中使用了 this 关键字对属性 name、number 和 age 赋值,this 表示当前对象。this.name=name语句表示一个赋值语句,等号左边的 this.name 是指当前对象具有的变量 name,等号右边的 name 表示参数传递过来的数值。
- 创建一个 main() 方法对 Student 类进行测试,代码如下🍟
public class Main {
public static void main(String[] args) {
Student student = new Student("许嘉雯", 2103030203, 18);
System.out.println("学生信息如下:");
System.out.println("学生姓名:" + student.name + "\n学生学号:" + student.id + "\n学生年龄:" + student.age);
}
}
- 运行该程序,结果如下所示👇
总结
当一个类的属性(成员变量)名与访问该属性的方法参数名相同时,则需要使用 this 关键字来访问类中的属性,以区分类的属性和方法中的参数。构造方法里的同名变量就是通过
this区分。前面说过,this指“这个类”,所以this.变量名就是指的“这个类”的成员变量,而非形参中的变量,以此达到区分的目的
二、this.方法名🌭
this 关键字最大的作用就是让类中一个方法,访问该类里的另一个方法或实例变量。
例2
- 先看一个不好的栗子🍿
public class Student{
// 定义一个wakeUp()方法
public void wakeUp(){
System.out.println("起床了");
}
// 定义一个eat()方法,eat()方法需要依赖wakeUp()方法
public void eat(){
Student s = new Student();
s.wakeUp();
System.out.println("吃早饭");
}
}
- 下面再提供一个程序来创建 student 对象,并调用该对象的 eat( ) 方法🧂
public class Main {
public static void main(String[] args) {
// 创建student对象
Student student = new Student();
// 调用student对象的eat()方法
student.eat();
}
}
在上面的程序中,一共产生了两个 Student对象,在 Student类的eat( ) 方法中,程序创建了一个 Student对象,并使用名为 s 的引用变量来指向该 Student对象。在 Main的 main( ) 方法中,程序再次创建了一个 Student对象,并使用名为 student 的引用变量来指向该 Student对象。
这样不符合逻辑。这就相当于本对象的eat()方法,需要调用另一个对象的wakeUp()方法,我一个学生起床,却要让另一个学生替我去吃早饭。
- 再看一下这个栗子🥓
public class Student{
// 定义一个wakeUp()方法
public void wakeUp(){
System.out.println("起床了");
}
// 定义一个eat()方法,eat()方法需要依赖wakeUp()方法
public void eat(){
this.wakeUp(); // 使用this引用调用eat()方法的对象
System.out.println("吃早饭");
}
}
这样就符合逻辑了,自己起床自己吃早饭。
- Java允许同一个对象的方法直接调用该对象的属性或者方法,所以this可以省略🥚
public void eat(){
wakeUp(); // 等同于this.wakeUp()
System.out.println("吃早饭");
}
- 运行程序,结果如下👇
总结
在现实世界里,对象的一个方法依赖于另一个方法的情形很常见,例如,吃早饭方法依赖于起床方法,写作业方法依赖于吃饭方法(PS:不吃饭就没力气学习😁) 这种依赖都是同一个对象两个方法之间的依赖。
三、this( )访问构造方法🍳
在处理构造函数重载时,我们可能必须从另一个构造函数调用一个构造函数。在这种情况下,我们不能显式调用构造函数。相反,我们必须使用this关键字。
在这里,我们使用this关键字的另一种形式,也就是this()。
注意点
- this( ) 不能在普通方法中使用,只能写在构造方法中
- 在构造方法中使用时,必须是第一条语句
- 调用时参数必须严格匹配
例 3
- 创建一个学生类🧇
public class Student {
public String name;
public int age;
//带两个参数的构造方法
public Student(String name, int age) {
this.name = name;
this.age = age;
}
//单个参数的构造方法
public Student(int age) {
//用两个参数调用构造方法
this("无名氏", age);
}
//无参构造方法
public Student() {
//用单个参数调用构造方法
this(0);
}
// 输出name和age
public String toString() {
return this.name + " : " + this.age + "岁";
}
}
- 写一个主方法测试一下🥞
public class Main {
public static void main( String[] args ) {
//创建Student类的对象
//使用2个参数调用构造方法
Student s1 = new Student("许嘉雯", 18);
//使用单个参数调用构造方法
Student s2 = new Student(20);
//不带参数调用构造方法
Student s3 = new Student();
//打印对象
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
- 运行结果如下👇
总结
这种调用方式的优点在于一个构造器可以不必重复编写其他构造器中已有的代码,而是通过调用其他构造函数以实现复用,从而提供良好和类代码结构。
写在后面🍻
感谢观看啦✨
有什么不足,欢迎指出哦💖
掘金的运营同学审核辛苦了💗