How to use the Java this keyword

How to use the Java this keyword

This keyword in constructor

Construction methods are automatically called when a class object is created through the new keyword. In the program, they cannot be called through the method name (that is, the class name) like other methods. However, if a class has multiple construction methods, other construction methods can be called through this (para...) in one construction method. Using this to call other construction methods has the following constraints.

1) Other construction methods can only be called through this in the construction method, which 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:

The test (int) construction method is invoked in the test () method, and the test (int) construction method calls the test () construction method to form a recursive call. Test (int, int) calls itself and forms recursive calls. Are not allowed.

3) Calling other constructor methods through this must be executed in the first line of the constructor. Since the constructor of super calling the parent class must also be executed in the first line of the constructor, the constructor calling this and super cannot appear in one constructor at the same time. You cannot call different constructor methods multiple times in a constructor. In the constructor, you can also use the this keyword to access the member variables and member functions in this class. Its usage is the same as this keyword in non constructor methods.

This keyword in non construction method

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 (2) this. In the class yyy(paras…); Access the member method YYY 3) this in the class; Reference to the current class object

This keyword is not controlled by access permission when accessing member variables and member functions of a class. You can access all member variables and methods in this class, including private member variables and methods. Static members of this class can also be accessed through this. However, since static members can be accessed directly through the class name, if accessed through this, there will be "the static field" ××× "Should be accessed in a static way". You cannot use this in a static member or static block of a class.

This keyword under inheritance relationship

Under inheritance, the this keyword in the parent class does not always represent variables and methods in the parent class. The four uses of this keyword are described above and listed below.

1) this(paras…); Access other construction methods 2) this xxx; Access the member variable XXX (3) this. In the class yyy(paras…); Access the member method YYY 4) this in the class; Reference to the current class object

For the first, this (para...) is the construction method regardless of whether the subclass has the same parameters; The constructor in the parent class is always accessed. For the second, whether or not the subclass overrides the member variable, this xxx; Always access member variables in the parent class. For the third, if the subclass overrides the member method, this yyy(paras…); The member method of the subclass is accessed. If the subclass does not override the member method, this yyy(paras…); The member method of the parent class is accessed. For the fourth, this always represents the object of the subclass.

For example:

There are two statements in the main() function, new child() and child show()。

When the first statement new child(), the constructor of child class will be executed, but child class is a subclass of parent class, so the constructor of parent class will be executed first. The parameterless constructor of 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. In the parent parameter Parent (), the this (1) is called. This call represents a construction method for executing an integer parameter in the parent class, although there is also a method of constructing an integer parameter in the subclass, but it will not be executed. The constructor with an integer parameter in the parent class executes this STR = "parent", this here STR represents the member variable STR in the parent class. Although there is also a member variable STR in the child class, it will not be assigned. After assigning the member variable STR in the parent class to "parent", then execute this Show(), although there is a show() method in the parent class, because the child class 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, the STR in the subclass is obviously printed. The str of the subclass is not assigned 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, and this is printed in the show() method of the parent class STR operation, this STR also represents the member variable STR in the parent class, so print "parent".

The second statement is child Show () first executes the show () method of the subclass. Show () of the subclass first prints the str value of the subclass (null), and then executes show () of the parent class to print the str value of the parent class ("parent").

The print results of the two statements are null, parent, null, parent.

If the first statement is changed to new child (1), the construction method of the subclass with an integer parameter is executed, and the parameterless construction method of the parent class is still executed first, the str of the parent class is initialized to "parent", then the show() of the subclass is executed, the show() of the subclass prints the str value (null) of the subclass, and then the show() of the parent class is executed, The parent class show() prints the str value of the parent class ("parent"), and then executes the construction method of the child class to initialize the str of the child class to "child". The second statement child. Show() first executes the show() method of the child class, and the show() of the child class first prints the str value of the child class ("child"), and then executes the show() of the parent class to print the str value of the parent class ("parent").

The print results of the two statements are null, child and parent.

Similarities and differences between super and this

Super is used to refer to the members of its parent class in a class. It is a bridge to access the members of the parent class in the child class, not a reference to any object, while this represents the reference to the current class object. In the code, object o = Super; Is wrong, object o = this; Is allowed. The super keyword is used to access the member variables and methods of the parent class when the child class overrides a member variable of the parent class or overrides a member method of the parent class. If the subclass does not override the member variables and member methods of the parent class, the subclass will inherit all non private member variables and member methods of the parent class. At this time, in the subclass, the result is the same whether the member is accessed through this or super.

super. GetClass () and this getClass()

GetClass () is a final method defined by the object class. GetClass () of all Java classes inherits from the object class. As mentioned earlier, if the subclass does not override a member method of the parent class, the result is the same whether it is accessed through super or this. Therefore, super GetClass () and this The result of getClass () is the same. The getClass () method of object class returns the runtime class of the object. The runtime class of an object is the class specified when the object is created through new. Therefore, super GetClass () and this Getclass() returns all the classes specified in the new object.

For example:

The printed class names are child.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>