在java中,this
关键字有很多种用法。 在java中,这是一个引用当前对象的引用变量。
java this
关键字的用法如下:
this
关键字可用来引用当前类的实例变量。this
关键字可用于调用当前类方法(隐式)。this()
可以用来调用当前类的构造函数。this
关键字可作为调用方法中的参数传递。this
关键字可作为参数在构造函数调用中传递。this
关键字可用于从方法返回当前类的实例。建议:如果你是java初学者,只学习
this
关键字的前三个用法就可以了。
this
关键字可以用来引用当前类的实例变量。如果实例变量和参数之间存在歧义,则 this
关键字可用于明确地指定类变量以解决歧义问题。
了解没有 this 关键字的问题
下面先来理解一个不使用 this
关键字的示例:
class student {
int rollno;
string name;
float fee;
student(int rollno, string name, float fee) {
rollno = rollno;
name = name;
fee = fee;
}
void display() {
system.out.println(rollno + " " + name + " " + fee);
}
}
class testthis1 {
public static void main(string args[]) {
student s1 = new student(111, "ankit", 5000f);
student s2 = new student(112, "sumit", 6000f);
s1.display();
s2.display();
}
}
执行上面代码输出结果如下 -
0 null 0.0
0 null 0.0
在上面的例子中,参数(形式参数)和实例变量(rollno
和name
)是相同的。 所以要使用this
关键字来区分局部变量和实例变量。
使用 this 关键字解决了上面的问题
class student {
int rollno;
string name;
float fee;
student(int rollno, string name, float fee) {
this.rollno = rollno;
this.name = name;
this.fee = fee;
}
void display() {
system.out.println(rollno + " " + name + " " + fee);
}
}
class testthis2 {
public static void main(string args[]) {
student s1 = new student(111, "ankit", 5000f);
student s2 = new student(112, "sumit", 6000f);
s1.display();
s2.display();
}
}
执行上面代码输出结果如下 -
111 ankit 5000
112 sumit 6000
如果局部变量(形式参数)和实例变量不同,则不需要像下面的程序一样使用this
关键字:
不需要 this 关键字的程序示例
class student {
int rollno;
string name;
float fee;
student(int r, string n, float f) {
rollno = r;
name = n;
fee = f;
}
void display() {
system.out.println(rollno + " " + name + " " + fee);
}
}
class testthis3 {
public static void main(string args[]) {
student s1 = new student(111, "ankit", 5000f);
student s2 = new student(112, "sumit", 6000f);
s1.display();
s2.display();
}
}
执行上面代码输出结果如下 -
111 ankit 5000
112 sumit 6000
对变量使用有意义的名称是一种好的编程习惯。所以使用相同名称的实例变量和参数,并且总是使用
this
关键字。
可以使用this
关键字调用当前类的方法。如果不使用this
关键字,编译器会在调用方法时自动添加此 this
关键字。我们来看看这个例子。
执行上面代码输出结果如下 -
hello n
hello m
this()
构造函数调用可以用来调用当前类的构造函数。 它用于重用构造函数。 换句话说,它用于构造函数链接。
从参数化构造函数调用默认构造函数:
class a {
a() {
system.out.println("hello a");
}
a(int x) {
this();
system.out.println(x);
}
}
class testthis5 {
public static void main(string args[]) {
a a = new a(10);
}
}
执行上面代码输出结果如下 -
hello a
10
从默认构造函数调用参数化构造函数:
class a {
a() {
this(5);
system.out.println("hello a");
}
a(int x) {
system.out.println(x);
}
}
class testthis6 {
public static void main(string args[]) {
a a = new a();
}
}
执行上面代码输出结果如下 -
5
hello a
this()
构造函数调用用于从构造函数重用构造函数。 它维护构造函数之间的链,即它用于构造函数链接。看看下面给出的示例,显示this
关键字的实际使用。
class student {
int rollno;
string name, course;
float fee;
student(int rollno, string name, string course) {
this.rollno = rollno;
this.name = name;
this.course = course;
}
student(int rollno, string name, string course, float fee) {
this(rollno, name, course);// reusing constructor
this.fee = fee;
}
void display() {
system.out.println(rollno + " " + name + " " + course + " " + fee);
}
}
class testthis7 {
public static void main(string args[]) {
student s1 = new student(111, "ankit", "java");
student s2 = new student(112, "sumit", "java", 6000f);
s1.display();
s2.display();
}
}
执行上面代码输出结果如下 -
111 ankit java null
112 sumit java 6000
注意:调用
this()
必须是构造函数中的第一个语句。
下面示例为不把 this()
语句放在第一行,因此编译不通过。
class student {
int rollno;
string name, course;
float fee;
student(int rollno, string name, string course) {
this.rollno = rollno;
this.name = name;
this.course = course;
}
student(int rollno, string name, string course, float fee) {
this.fee = fee;
this(rollno, name, course);// c.t.error
}
void display() {
system.out.println(rollno + " " + name + " " + course + " " + fee);
}
}
class testthis8 {
public static void main(string args[]) {
student s1 = new student(111, "ankit", "java");
student s2 = new student(112, "sumit", "java", 6000f);
s1.display();
s2.display();
}
}
执行上面代码输出结果如下 -
compile time error: call to this must be first statement in constructor
this
关键字也可以作为方法中的参数传递。 它主要用于事件处理。 看看下面的一个例子:
class s2 {
void m(s2 obj) {
system.out.println("method is invoked");
}
void p() {
m(this);
}
public static void main(string args[]) {
s2 s1 = new s2();
s1.p();
}
}
执行上面代码输出结果如下 -
method is invoked
这个应用程序可以作为参数传递:
在事件处理(或)的情况下,必须提供一个类的引用到另一个。 它用于在多个方法中重用一个对象。
也可以在构造函数中传递this
关键字。 如果必须在多个类中使用一个对象,可以使用这种方式。 看看下面的一个例子:
class b {
a4 obj;
b(a4 obj) {
this.obj = obj;
}
void display() {
system.out.println(obj.data);// using data member of a4 class
}
}
class a4 {
int data = 10;
a4() {
b b = new b(this);
b.display();
}
public static void main(string args[]) {
a4 a = new a4();
}
}
执行上面代码输出结果如下 -
10
可以从方法中 this
关键字作为语句返回。 在这种情况下,方法的返回类型必须是类类型(非原始)。 看看下面的一个例子:
作为语句返回的语法
return_type method_name(){
return this;
}
从方法中返回为语句的 this 关键字的示例
class a {
a geta() {
return this;
}
void msg() {
system.out.println("hello java");
}
}
class test1 {
public static void main(string args[]) {
new a().geta().msg();
}
}
执行上面代码输出结果如下 -
hello java
验证 this 关键字
现在来验证 this
关键字引用当前类的实例变量。 在这个程序中将打印参考变量,这两个变量的输出是相同的。
class a5 {
void m() {
system.out.println(this);// prints same reference id
}
public static void main(string args[]) {
a5 obj = new a5();
system.out.println(obj);// prints the reference id
obj.m();
}
}
执行上面代码输出结果如下 -
a5@22b3ea59
a5@22b3ea59