目录
this关键字
this关键字指向的是类的实例对象,我们可以通过以下代码证明这点,下面打印出来的值是一样的
public class PrintThis {
void display() {
System.out.println(this);
}
}
class PrintThisTest {
public static void main(String[] args) {
PrintThis p = new PrintThis();
System.out.println(p);
p.display();
}
}
this关键字在Java中主要有以下几种用法
- this可以指向当前类的实例变量
- this可以调用当前类的方法
- this()可以用来调用当前类的构造方法
- this可以用于方法的传参
- this可以用于构造方法的传参
- this可以通过方法返回当前类的实例
1)this可以指向当前类的实例变量
首先让我们看看下面不使用this关键字的例子
public class Student {
//实例变量
String name;
int age;
//构造方法
public Student(String name, int age) {
//局部变量
name = name;
age = age;
}
void display() {
System.out.println("Student " + name + " is: " + age);
}
}
class StudentTest{
public static void main(String[] args) {
Student student = new Student("Alice", 18);
student.display();
}
}
代码输出:
Student null is: 0
看到输出结果,不禁让人心生疑惑,为什么会这样呢?我们的期望的输出不应该是Student Alice is: 18才对吗?
代码详解:
- 下面代码表示我们实例化一个对象student,里面有两个初始化的参数传给构造方法,"Alice"对应构造方法的name参数,18对应构造方法的age参数
- 构造方法里面的变量属于局部变量,赋值以后也只能在当前方法体内使用,因此不会对实例变量有任何影响
Student student = new Student("Alice", 18);
所以执行为这段代码后,变量赋值情况如下:
String name; name=null 因为String对象的默认值是null
int age; age = 0 int类型的默认值是0
public Student(String name, int age) { name="Alice" age=18
name = name; name = "Alice"
age = age; age = 18
}
最后当我们调用display()方法时,拿到的变量是实例变量name和age,所以代码最后的输出结果是:Student null is: 0
代码修改如下后再运行:
public Student(String name, int age) {
this.name = name;
this.age = age;
}
代码输出:
Student Alice is: 18
this.name和this.age指向的是实例变量,将构造方法的变量值赋值给实例变量
2)this可以调用当前类的方法
this关键字可以用来调用当前类的方法,如果不使用this关键字的话,编译器会自动在方法前面加上this关键字
源代码文件:ThisMethod.java
public class ThisMethod {
void methodA() {
System.out.println("方法A");
}
void methodB(){
System.out.println("方法B");
//第一种调用方式:不使用this关键字
methodA();
//第二种调用方式:使用this关键字
this.methodA();
}
}
class ThisMethodTest{
public static void main(String[] args) {
ThisMethod t = new ThisMethod();
t.methodB();
}
}
编译后的文件:ThisMethod.class
public class ThisMethod {
public ThisMethod() {
}
void methodA() {
System.out.println("方法A");
}
void methodB() {
System.out.println("方法B");
this.methodA();
this.methodA();
}
}
调用本类的方法需不需要在方法前面加this,看个人喜好
- 使用this调用当前类的方法,代码看得更加清晰,表示使用的是当前类的方法
- 不使用this调用当前类的方法,代码看起来会更简洁
3)this()可以用来调用当前类的构造方法
this()常常用来指向同类中的构造方法,增加代码的复用性
public class ThisConstructor {
String name;
int age;
String sex;
public ThisConstructor(String name, int age) {
this.name = name;
this.age = age;
}
public ThisConstructor(String name, int age, String sex) {
// 下面这种写法也可以
// this.name = name;
// this.age = age;
// this.sex =sex;
this(name, age);
this.sex = sex;
}
public void displayA() {
System.out.println(name + " " + age);
}
public void displayB() {
System.out.println(name + " " + age + " " + sex);
}
}
class ThisConstructorTest{
public static void main(String[] args) {
ThisConstructor t1 = new ThisConstructor("Alice", 18);
t1.displayA();
ThisConstructor t2 = new ThisConstructor("Bob", 19, "Boy");
t2.displayB();
}
}
4)this可以用于方法的传参
this关键字也可以作为方法中的参数传递,主要用于事件处理
public class ThisMethod {
void methodA(ThisMethod obj) {
obj.methodC();
}
void methodB(){
methodA(this);
}
void methodC(){
System.out.println("方法C");
}
}
class ThisMethodTest{
public static void main(String[] args) {
ThisMethod t = new ThisMethod();
t.methodB();
}
}
5)this可以用于构造方法的传参
public class ThisConstructorArgs {
ThisConstructorArgsTest obj;
ThisConstructorArgs(ThisConstructorArgsTest obj){
this.obj = obj;
}
void display() {
System.out.println(obj.data);
}
}
class ThisConstructorArgsTest{
int data = 100;
ThisConstructorArgsTest() {
ThisConstructorArgs t = new ThisConstructorArgs(this);
t.display();
}
public static void main(String[] args) {
ThisConstructorArgsTest test = new ThisConstructorArgsTest();
}
}
6)this可以通过方法返回当前类的实例
如果需要返回当前实例对象的方法,可以使用return this返回当前实例对象
public class ThisCurrentClass {
ThisCurrentClass getThisCurrentClass(){
return this;
}
void display() {
System.out.println("this可以通过方法返回当前类的实例");
}
}
class ThisCurrentClassTest{
public static void main(String[] args) {
ThisCurrentClass test = new ThisCurrentClass();
test.getThisCurrentClass().display();
}
}