This article mainly introduces the relevant information on how to use the Java this keyword in detail. I hope this article can help everyone and let everyone fully understand this part of the content. Friends in need can refer to it
Detailed explanation of the use of Java this keyword
This keyword in the constructor
The constructor is an object of a class that passes the new key It is automatically called when the word is created. It cannot be called through the method name (that is, the class name) like other methods in the program. But if a class has multiple constructors, you can call other constructors through this (paras...) in one constructor.
Using this to call other constructors has the following constraints.
1) Other constructors can only be called through this in the constructor, and cannot be used in ordinary methods.
2) The constructor cannot be called recursively through this, that is, the constructor itself cannot be called directly or indirectly through this in a constructor.
For example:
class test { test() { this(1); } test(int a){ this(); } test(int a, int b) { this(1, 2); } }
The test(int) constructor is called in the test() method, and the test(int) constructor is called The test() constructor is added to form a recursive call. Calling itself in test(int, int) also constitutes a recursive call. All are not allowed.
3) Calling other constructors through this must be executed in the first line of the constructor. Since super calls the constructor of the parent class must also be executed in the first line of the constructor, therefore, calling the constructor through this and super cannot appear in the same constructor at the same time. You cannot call different constructors multiple times in one constructor.
You can also use the this keyword in the constructor to access member variables and member functions in this class. Its usage is the same as the this keyword in non-constructor methods.
This keyword in non-constructor methods
In Java, you can call member variables and methods in a class through the this keyword. Its usage is.
1) this.xxx; access the member variable xxx in the class
2) this.yyy(paras…); access the member method yyy in the class
3) this; currently References to class objects
The this keyword is not subject to access permission control when accessing member variables and member functions of the class. It can access all member variables and methods in this class, including private member variables and method. You can also access static members of this class through this. However, since static members can be directly accessed through the class name, if you access through this, there will be a warning message "The static field ××× should be accessed in a static way". This cannot be used within a static member of a class or within a static block.
This keyword under inheritance relationship
Under inheritance relationship, the this keyword in the parent class does not always represent the variables and methods in the parent class. The four uses of this keyword are as mentioned above and are listed below.
1) this(paras…); Access other constructors
2) this.xxx; Access member variables xxx in the class
3) this.yyy(paras…) ; Access member methods in the classyyy
4) this; Reference to the current class object
For the first type, regardless of whether the subclass has a constructor with the same parameters, this(paras...) ;The access is always to the constructor in the parent class.
For the second type, regardless of whether the subclass covers the member variable, this.xxx; always accesses the member variable in the parent class.
For the third type, if the subclass overrides the member method, then this.yyy(paras...); accesses the member method of the subclass. If the subclass does not override the member method, then this.yyy (paras...); accesses the member methods of the parent class.
For the fourth type, this always represents the object of the subclass.
For example:
public class ClassTest { public static void main(String[] args) { Child child = new Child(); child.show(); } } class Parent { public String str; Parent(){ this(1); } Parent(int a) { this.str = "Parent"; this.show(); } public void show() { System.out.println(this.str); } } class Child extends Parent { public String str; Child() { } Child(int a) { str = "Child"; } public void show() { System.out.println(str); super.show(); } }
There are two statements in the main() function, new Child() and child.show().
When the first statement new Child() is executed, the constructor of the Child class will be executed. However, the Child class is a subclass of the Parent class, so the constructor of the Parent class will be executed first. The parameterless constructor of the Child class does not use super and this to call the parent class or other constructors in this class, so the parameterless constructor of the parent class will be called. This(1) is called and executed in the parameterless constructor Parent() of the parent class. This call means executing the constructor with an integer parameter in the parent class. Although there is also a constructor with an integer parameter in the subclass, But it will not be executed. The constructor method with an integer parameter in the parent class executes this.str="Parent". This.str here represents the member variable str in the parent class. Although there is also a member variable str in the subclass, it will not be used. Assignment. After assigning the member variable str in the parent class to "Parent", then this.show() is executed. Although there is a show() method in the parent class, because the subclass overrides the show() method, this. The show() method of the subclass executed by show(). The show() method of the subclass first performs the operation of printing str. At this time, what is printed is obviously the str in the subclass. The str of the subclass is not assigned a value because null is printed. Then the show() method of the subclass executes super.show(), that is, the show() method of the parent class is called. In the show() method of the parent class, the operation of printing this.str is performed. This.str also represents The member variable str in the parent class, so "Parent" is printed.
第二条语句child.show()先是执行子类的show()方法,子类的show()先是打印了子类的str值(null),然后执行了父类的show()打印了父类的str值(”Parent”)。
两条语句的打印结果为null, Parent, null, Parent。
如果将第一条语句改为new Child(1),则执行的是子类的有一个整数参数的构造方法,仍然是先执行父类的无参构造方法,初始化父类的str为”Parent”,然后执行子类的show(),子类的show()打印子类的str值(null),然后执行父类的show(),父类show()打印父类的str值(”Parent”),然后执行子类的构造方法将子类的str初始化为”Child”。 第二条语句child.show()先是执行子类的show()方法,子类的show()先是打印了子类的str值(”Child”),然后执行了父类的show()打印了父类的str值(”Parent”)。
两条语句的打印结果为null, Parent, Child, Parent。
super和this的异同
super在一个类中用来引用其父类的成员,它是在子类中访问父类成员的一个桥梁,并不是任何一个对象的引用,而this则表示当前类对象的引用。在代码中Object o = super;是错误的,Object o = this;则是允许的。
super关键字的作用在于当子类中覆盖了父类的某个成员变量,或者重写了父类的某个成员方法时还能够访问到父类的成员变量和成员方法。如果子类中没有重写父类的成员变量和成员方法,则子类会继承父类的所有非private的成员变量和成员方法。这时在子类中无论通过this来访问成员和通过super来访问成员,结果都是一样的。
super.getClass()和this.getClass()
getClass()是Object类定义的一个final方法,所有Java类的getClass()都继承自Object类。如前文所述,如果子类没有重写父类的某个成员方法,那么通过super来访问还是还是通过this来访问结果都是一样的。因此,super.getClass()和this.getClass()结果是一样的。Object类的getClass()方法返回的是该对象的运行时类,一个对象的运行时类是该对象通过new创建时指定的类。因此,super.getClass()和this.getClass()返回的都是new对象时指定的类。
例如:
public class ClassConstructorTest { public static void main(String[] args) { Child child = new Child(); child.show(); } } class Parent { private Parent mSelf; Parent(){ mSelf = this; } public void show() { System.out.println(this.getClass().getName()); System.out.println(super.getClass().getName()); System.out.println(mSelf.getClass().getName()); } } class Child extends Parent { public void show() { System.out.println(this.getClass().getName()); System.out.println(super.getClass().getName()); super.show(); } }
打印的类名都是Child。
The above is the detailed content of Detailed explanation of the use of this keyword in Java. For more information, please follow other related articles on the PHP Chinese website!