In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "what can be done with This". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the work done with This"?
01. Point to the current object
"third sister, take a look at the following code." As soon as the words fell, I crackled on the keyboard.
Public class WithoutThisStudent {String name; int age; WithoutThisStudent (String name, int age) {name = name; age = age;} void out () {System.out.println (name+ "" + age);} public static void main (String [] args) {WithoutThisStudent S1 = new WithoutThisStudent (Silent King II, 18) WithoutThisStudent S2 = new WithoutThisStudent (Silent King 3, 16); s1.out (); s2.out ();}}
"in the above example, the parameter name of the constructor is the same as the instance variable name, and the instance variable cannot be assigned because the this keyword is not used." I raised the index finger of my right hand, pointed to name and age on the screen and said to the third sister.
"take a look at the output of the program."
Null 0 null 0
"you can see from the result that although parameters are passed when the object is created, the instance variables are not assigned. This is because if the this keyword is not used in the constructor, name and age do not point to the instance variables but to the parameters themselves." I twisted my neck to the right, looked at the third sister and said.
"then how to solve this problem? brother." The third sister asked anxiously.
"if the parameter name conflicts with the instance variable name." I was about to give an answer when the third sister interrupted me.
"do you use this?" The third sister blurted out.
"Wow, it's getting better and better, you." I feel that the third sister gradually has her own willingness to think on her own initiative on the road of learning Java.
"Yes, take a look at the code with the this keyword added."
There was another crackle of keyboards in the quiet room.
Public class WithThisStudent {String name; int age; WithThisStudent (String name, int age) {this.name = name; this.age = age;} void out () {System.out.println (name+ "" + age);} public static void main (String [] args) {WithThisStudent S1 = new WithThisStudent (Silent King II, 18) WithThisStudent S2 = new WithThisStudent (Silent King 3, 16); s1.out (); s2.out ();}}
"Let's take a look at the output of the program."
Silent King 2 18 Silent King 3 16
"this time, the instance variable has a value, and in the constructor, this.xxx points to the instance variable, not the parameter itself." I said slowly, "of course, you don't have to use the this keyword if the parameter name is different from the instance variable name, but I recommend using the this keyword, which makes more sense."
03. Call the method of the current class
"listen carefully, third sister, and see if I can hit the keyboard fast enough."
Public class InvokeCurrentClassMethod {void method1 () {} void method2 () {method1 ();} public static void main (String [] args) {new InvokeCurrentClassMethod () .method1 ();}}
"look carefully, third sister, don't you see the keyword this in the above code?" With a mysterious smile on my face, I was going to do a magic trick for my third sister.
"not really, brother. I confirmed it."
"then something amazing is going to happen." I suddenly felt that Liu Qian was possessed.
I quickly find the InvokeCurrentClassMethod.class file in the classes directory and double-click to open it (IDEA will open the bytecode file using FernFlower by default).
Public class InvokeCurrentClassMethod {public InvokeCurrentClassMethod () {} void method1 () {} void method2 () {this.method1 ();} public static void main (String [] args) {(new InvokeCurrentClassMethod ()) .method1 ();}}
"keep your eyes open and look carefully, third sister, does the this keyword appear?"
"Wow, really, it's amazing!" The third sister worked hard in order to cooperate with my performance.
"We can use the this keyword in one class to call another method, and if not, the compiler will automatically add it for us." I am confident of my deep programming skills. "in the source code, method2 () does not use the this keyword when calling method1 (), but it can be seen through the decompiled bytecode."
04. Call the constructor of the current class
"Let's look at the following code."
Public class InvokeConstrutor {InvokeConstrutor () {System.out.println ("hello");} InvokeConstrutor (int count) {this (); System.out.println (count);} public static void main (String [] args) {InvokeConstrutor invokeConstrutor = new InvokeConstrutor (10);}}
"in the parametric constructor InvokeConstrutor (int count), this () is used to call the parameterless constructor InvokeConstrutor ()." This time, I switched to the index finger of my left hand, pointed to the screen and said to my third sister, "this () can be used to call the constructor of the current class-the constructor can be reused."
"Let's take a look at the output."
Hello 10
"really, the no-parameter constructor is also called, so the program outputs hello." The third sister said without thinking when she saw the output result.
"you can also use this () in a no-parameter constructor and pass parameters to call the parameterized constructor." When the words fell, I tapped on the keyboard, "look at the following code."
Public class InvokeParamConstrutor {InvokeParamConstrutor () {this (10); System.out.println ("hello");} InvokeParamConstrutor (int count) {System.out.println (count);} public static void main (String [] args) {InvokeParamConstrutor invokeConstrutor = new InvokeParamConstrutor ();}}
"Let's take a look at the output of the program."
10 hello
"however, it is important to note that this () must be placed on the first line of the constructor, or else an error will be reported."
05. Passed as a parameter in the method
"look at the following code."
Public class ThisAsParam {void method1 (ThisAsParam p) {System.out.println (p);} void method2 () {method1 (this);} public static void main (String [] args) {ThisAsParam thisAsParam = new ThisAsParam (); System.out.println (thisAsParam); thisAsParam.method2 ();}}
The this keyword can be passed as an argument in a method, where it points to the object of the current class. Accidentally, half an hour later, I felt a smoke in my throat, so I quickly took another sip of water to moisten my throat and continued.
"take a look at the output, and you'll see, third sister."
Com.itwanger.twentyseven.ThisAsParam@77459877 com.itwanger.twentyseven.ThisAsParam@77459877
"method2 () calls method1 () and passes the string of the current object in the parameter this,method1 (). The string of the thisAsParam object is printed in the main () method. You can see from the output that the two are the same object."
06. Passed as a parameter in the construction method
"keep looking at the code."
Public class ThisAsConstrutorParam {int count = 10; ThisAsConstrutorParam () {Data data = new Data (this); data.out ();} public static void main (String [] args) {new ThisAsConstrutorParam ();}} class Data {ThisAsConstrutorParam param; Data (ThisAsConstrutorParam param) {this.param = param;} void out () {System.out.println (param.count) }}
"in the constructor ThisAsConstrutorParam (), we use the this keyword as an argument to the Data object, which actually points to the object new ThisAsConstrutorParam ()."
"the this keyword can also be passed as an argument in the constructor, pointing to the object of the current class. This is useful when we need to use an object in multiple classes."
"Let's take a look at the output."
ten
07. As the return value of the method
"do you need a rest? third sister."
"it's all right, brother. I'm still very focused. Go on."
"all right, let's keep looking at the code."
Public class ThisAsMethodResult {ThisAsMethodResult getThisAsMethodResult () {return this;} void out () {System.out.println ("hello");} public static void main (String [] args) {new ThisAsMethodResult (). Out ();}}
"the getThisAsMethodResult () method returns the this keyword, pointing to the object new ThisAsMethodResult (), so you can immediately call the out () method-- achieving the purpose of chained calls, which is a classic use of the this keyword."
"chained calls are more common in JavaScript code." In order to prove this to the third sister, I opened the source code of jQuery.
"there are so many chain calls!" The third sister sighed.
"Yes." I nodded and pointed to the return value of the getThisAsMethodResult () method and said to the third sister, "it is important to note that when the this keyword is used as the return value of the method, the return type of the method is the class type."
"Let's take a look at the output."
Hello
"so, that's the end of the introduction to the this keyword." After moving my stiff neck, I said to my third sister, "if you can study hard enough, how about we carry the super keyword along with you?"
"No, I heard that the super keyword is simpler. I'll just see for myself. You don't have to talk about it!"
"No, third sister, you have to pretend to listen, otherwise how can I hand over the work to the readers?"
"Oh." The third sister smiled meaningfully.
08, super keyword
"there are three main uses of the super keyword."
Point to the parent object
Call the method of the parent class
Super () can call the constructor of the parent class.
"in fact, it is somewhat similar to this, but with different intentions." I picked up the water bottle and took a few more gulps, thirsty. "whenever a subclass object is created, the parent object is also implicitly created, which is referenced by the super keyword."
"if the parent and child classes have fields with the same name, the super keyword can be used to access the fields of the parent class with the same name."
"look at the following code."
Public class ReferParentField {public static void main (String [] args) {new Dog (). PrintColor ();} class Animal {String color = "white";} class Dog extends Animal {String color = "black"; void printColor () {System.out.println (color); System.out.println (super.color);}}
"there is a field named color in the parent class Animal and a field named color in the subclass Dog. In the subclass's printColor () method, the color of the parent class can be accessed through the super keyword."
"Let's take a look at the output."
Black and white
"when the method names of the subclass and the parent class are the same, you can use the super keyword to call the method of the parent class. In other words, the super keyword can be used to access the method of the parent class when the method is overridden."
Public class ReferParentMethod {public static void main (String [] args) {new Dog (). Work ();} class Animal {void eat () {System.out.println ("eat...");}} class Dog extends Animal {@ Override void eat () {System.out.println ("eat...") } void bark () {System.out.println ("woof...");} void work () {super.eat (); bark ();}}
"look, third sister. Both the parent class Animal and the child class Dog have a method called eat (), and the parent class's eat () method can be accessed through super.eat ()."
When the third sister digested herself, I typed another string of code on the keyboard.
Public class ReferParentConstructor {public static void main (String [] args) {new Dog ();}} class Animal {Animal () {System.out.println ("here comes the animal");}} class Dog extends Animal {Dog () {super (); System.out.println ("here comes the dog");}}
"in the constructor of the subclass Dog, the first line of code is super (), which is used to call the constructor of the parent class."
"Let's take a look at the output."
Here comes the animal. Here comes the dog.
"of course, by default, super () can be omitted, and the compiler will actively call the constructor of the parent class. That is, even if the subclass does not use super () to actively call the constructor of the parent class, the constructor of the parent class will still be executed first."
Public class ReferParentConstructor {public static void main (String [] args) {new Dog ();}} class Animal {Animal () {System.out.println ("here comes the animal");}} class Dog extends Animal {Dog () {System.out.println ("here comes the dog");}}
"the output is the same as before."
Here comes the animal. Here comes the dog.
"super () can also be used to call the parameterized constructor of the parent class, which improves the reusability of the code."
Class Person {int id; String name; Person (int id, String name) {this.id = id; this.name = name;}} class Emp extends Person {float salary; Emp (int id, String name, float salary) {super (id, name); this.salary = salary } void display () {System.out.println (id + "+ name +" + salary);}} public class CallParentParamConstrutor {public static void main (String [] args) {new Emp (1, Silent King II, 20000f). Display ();}}
"the Emp class inherits the Person class, that is, the id and name fields. When the salary field is added to the Emp, super (id, name) can be used in the constructor to call the parameter constructor of the parent class."
"Let's take a look at the output."
1 Silent Wang er 20000.0 has come here. I believe everyone has a deeper understanding of "what work has been done with This". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.