Several usage about this keyword

xiaoxiao2021-03-06  47

Two usages of this keyword

I think this is an important thing that implements multiplexing in Java. As I know, this keyword has two uses. First, use it to call the same name constructor; second, it refers to the instance object itself. We can check the following code:

Class accessthis {Object name; //} {this (null);} public referencethis (object name) {this.name = name;} // other method

This example can be seen in the two basic usage of this. Summary, we don't have to achieve a lot of constructors, and we only need to achieve once for the same configuration, meaningful constructor, and the parameter or attribute constructor only needs to be in the first sentence (here In the case, this can only be used in the first logical sentence, and the form of this (Field = Value) can be delegated to the construction method of the call parameter. This is also a very good embodiment of the reuse. The second usage is that in this example, it is embodied in the second constructor, that is, we don't have to worry about the variable name of our parameters and the variable name of the attribute, we can distinguish between joining the THIS identity, and The method of example is initialized.

转载请注明原文地址:https://www.9cbs.com/read-51558.html

New Post(0)