Use of 008-this

Disclaimer: All my articles are the compilation of online teaching videos, including Mad God Talk, Shang Silicon Valley, Dark Horse Programmer, etc., used as reference materials, without any commercial use, please refer to the majority of netizens, please do not spray ,Thank you. (Note that due to the website, some code characters may have problems. It is recommended that when reading the code, it is best to look at the corresponding picture below)

1. In Java, this keyword is difficult to understand, and its function is very close to its meaning.

1. It is used inside the method, that is, the reference of the object to which this method belongs;
2. It is used inside the constructor to indicate the object being initialized by the constructor.

Two, this can call the properties, methods and constructors of the class

3. When should this keyword be used?

When the object calling the method needs to be used in the method, use this. Specifically: We can use this to distinguish attributes and local variables, such as: this.name = name;
Example:
Use of 008-this
Explanation:
1. In any method or constructor, if you use the member variable or member method of the current class, you can precede it Add this to enhance the readability of the program. However, usually we are used to omit this.
2. When the form participates in the member variable with the same name, if you need to use the member variable in the method or constructor, you must add this to indicate that the variable is a member variable of the class.
3. When using this to access properties and methods, if they are not found in this class, they will be searched from the parent class.

Fourth, the current object

The object currently operating this method is called the current object.
Example:
Use of 008-this

Five, use this to call the constructor of this class

This can be used as an
example of a special format for mutual calling of constructors in a class :
Use of 008-this
Note:
1. You can use "this (parameter list)" in the constructor of the class to call other constructors overloaded in this class!
2. Clear: The constructor cannot call its own constructor by "this (parameter list)".
3. If n constructors are declared in a class, then at most n-1 constructors use "this" (Formal parameter list)"
4. "This (formal parameter list)" must be declared in the first line of the class constructor!
5. In a constructor of a class, only one "this (parameter list)" can be declared at most

Guess you like

Origin blog.51cto.com/12859164/2546661
use
use