教妹学Java(二十 七):this 关键字的用法,mybatis的原理和注解开发

教妹学Java(二十 七):this 关键字的用法,mybatis的原理和注解开发,第1张

教妹学Java(二十 七):this 关键字的用法,mybatis的原理和注解开发
  • 指向当前对象;

  • 调用当前类的方法

  • this() 可以调用当前类的构造方法;

  • this 可以作为参数在方法中传递;

  • this 可以作为参数在构造方法中传递;

  • this 可以作为方法的返回值,返回当前类的对象。

02、 指向当前对象

如果参数名和实例变量名产生了冲突,this 关键字可以解决这个问题。先来看一个没有 this 关键字的例子:

public class WithoutThisStudent {

String name;

int age;

WithoutThisStudent(String name, int age) {

name = name;

age = age;

}

void out() {

System.out.println(name+" " + age);

}

public static void main(String[] args) {

WithoutThisStudent s1 = new WithoutThisStudent(“沉默王二”, 18);

WithoutThisStudent s2 = new WithoutThisStudent(“沉默王三”, 16);

s1.out();

s2.out();

}

}

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

在上面的例子中,构造方法的参数名和实例变量名相同,由于没有使用 this 关键字,所以无法为实例变量赋值。程序输出的结果如下所示:

null 0

null 0

从结果中可以看得出来,尽管创建对象的时候传递了参数,但实例变量并没有赋值。这是因为如果构造方法中没有使用 this 关键字的话,name 和 age 指向的并不是实例变量而是参数。

怎么解决这个问题呢?加上 this 关键字就好了。

public class WithThisStudent {

String name;

int age;

WithThisStudent(String name, int age) {

this.name = name;

this.age = age;

}

void out() {

System.out.println(name+" " + age);

}

public static void main(String[] args) {

WithThisStudent s1 = new WithThisStudent(“沉默王二”, 18);

WithThisStudent s2 = new WithThisStudent(“沉默王三”, 16);

s1.out();

s2.out();

}

}

再来看一下程序的输出结果:

沉默王二 18

沉默王三 16

当然了,如果参数名和实例变量名不同的话,就不必使用 this 关键字,但我建议使用 this 关键字,这样的代码更有意义。

03、调用当前类的方法

我们可以在一个类中使用 this 关键字来调用另外一个方法,如果没有使用的话,编译器会自动帮我们加上。

public class InvokeCurrentClassMethod {

void method1() {}

void method2() {

method1();

}

public static void main(String[] args) {

new InvokeCurrentClassMethod().method1();

}

}

在源代码中,method2() 在调用 method1() 的时候并没有使用 this 关键字,但通过反编译后的字节码可以看得到。

public class InvokeCurrentClassMethod {

public InvokeCurrentClassMethod() {

}

void method1() {

}

void method2() {

this.method1();

}

public static void main(String[] args) {

(new InvokeCurrentClassMethod()).method1();

}

}

这次看到了吧?

04、调用当前类的构造方法

this() 可用于调用当前类的构造方法——构造方法可以重用了。

public class InvokeConstrutor {

InvokeConstrutor() {

System.out.println(“hello”);

}

InvokeConstrutor(int count) {

this();

System.out.println(count);

}

public static void main(String[] args) {

InvokeConstrutor invokeConstrutor = new InvokeConstrutor(10);

}

}

在有参构造方法 InvokeConstrutor(int count) 中,使用了 this() 来调用无参构造方法 InvokeConstrutor()。来看一下输出结果:

hello

10

果然,无参构造方法也被调用了,所以输出了“hello”。

也可以在无参构造方法中使用 this() 并传递参数来调用有参构造方法,来看下面这段代码:

public class InvokeParamConstrutor {

InvokeParamConstrutor() {

this(10);

System.out.println(“hello”);

}

InvokeParamConstrutor(int count) {

System.out.println(count);

}

public static void main(String[] args) {

InvokeParamConstrutor invokeConstrutor = new InvokeParamConstrutor();

}

}

来看一下程序的输出结果。

10

hello

注意,this() 必须放在构造方法的第一行,否则就报错了,见下图。

05、作为参数在方法中传递

this 关键字可以作为参数在方法中传递,它指向的是当前类的对象。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5687563.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存