In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what is java method and this keyword principle". In daily operation, I believe that many people have doubts about java method and this keyword principle. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "java method and this keyword principle". Next, please follow the editor to study!
Catalogue
Step 1. Add a way for customers to eat.
Step 2. Without adding static attributes and methods, you must first new the object.
Step 3. Use the object from new to execute the eat method
Step 4. How to understand c.eat ()
Step 5. Message receiver
Step 6. What if there are two customers?
Step 7, answer
Step 8. There is actually a this
Step 9. Use this directly in the eat method
Step 10. Construction method
Step 11. Summary: what is the meaning of this?
Step 12. I understand all the reasons, so what is static?
Step 13, complete source code of this section
Step 1. Add a way for customers to eat.
Suppose we have a customer class:
Class Customer {String name; / / customer name String sex; / / gender String birthDate; / / birthday String phoneNumber; / / phone number int status; / / customer status (1: normal, 2: abnormal) void eat () {System.out.println ("eating...") }}
Void represents that the method does not return a value.
Step 2. Without adding static attributes and methods, you must first new the object.
This is a formula, new means to create a new object, the Customer class is just a declarative thing, not a real object.
As long as you don't add static, you must create an object at run time before you can use it!
To put it arbitrarily, as long as you don't add static, you must new an object if you want to run the eat method.
As for why, I don't care for now, just write it down.
Step 3. Use the object from new to execute the eat method class Demo {public static void main (String [] args) {Customer c = new Customer (); c.eat ();}}
Run, you can see that the eat method is called.
Step 4. How to understand c.eat ()
C is a reference to a customer object, or pointer. C is a tag that does not hold the data of the customer object itself, but we manipulate c in the same way as we manipulate the customer object.
This line of code means that we sent a meal message to the customer. The customer was very smart and understood at once, so he used a period. The method is executed by adding eat (method name) and a parenthesis.
This is a successful communication.
Step 5. Message receiver
A method in a class, which we can call a message sink, is very easy to understand.
Step 6. What if there are two customers?
Let me ask you a question. If there are two customers, one is Arthur and the other is Angela. So whether the eat method they call is the same or different.
If this comes out of the examination question, it will be very differentiated.
Step 7, answer
The answer is the same way, I didn't expect it.
The reason is that the class loader loads the class file into the virtual machine, and the memory partition places all the methods of the class in a place called the method area. No matter how many objects you new, the same method is called. Because if you create a special memory space for each new object to store the method, it would be too wasteful.
Now there is a new question. Since the method is the same, how can I know who it is?
Step 8. There is actually a this
The truth is that...
Demo.java was compiled into Demo.class and Customer.class by the javac command, and then when running, the class loader first loaded Demo.class and found that Customer.class was also used, so Customer.class was also loaded by the way.
In this example, there are two areas in JVM that we need to focus on, one is the heap and the other is the method area.
What is a heap? A heap is an area where objects are stored. If you new one object at a time, the object is placed in the heap. For example, there is code like this:
Class Demo {public static void main (String [] args) {Customer C1 = new Customer (); Customer c2 = new Customer (); c1.name = "Arthur"; c2.name = "Angela";}}
We new two objects, where C1 and c2 are their references, and these two objects are placed in the heap.
And the eat method is placed in the method area, and there is only one copy.
And then the big deal is that when you use one of these objects to call the eat method, you actually pass a this.
C1.eat ()
This is passed into the eat method unknowingly as an argument to the eat method.
Every object has a this,this, which is not a property you set, but a tag that java sets to every class. You don't know it exists, but it's always there.
Step 9. Use this directly in the eat method
We use this directly in the eat method, which is what it looks like:
Void eat () {System.out.println (this.name + "eating...");}
Effect:
In fact, the bottom layer of it is like this, although we can't write this:
Void eat (this) {System.out.println (this.name + "eating...");}
We call a variable in the method, which is either passed in by the parameter or is a property of the class. Do you agree with this? Based on this common sense, the this is not the attribute we defined, so it can only be passed in by the parameter. However, this process is done by JVM, and we are not aware of it.
Step 10. Construction method
Every class has a constructor, which is written like this.
Public Customer () {}
This is an empty construction method, it's okay if you don't write, even if you don't write, there will be such a method. The function of the constructor is to call it directly when you new the object.
Remember a secret:
1. The class name is parenthesized = the constructor is called, and there must be a new on the left.
two。 The method name is parenthesized = this method is called, and a this is passed in by default (if there is no static)
3. Method name in parentheses = call this method without passing this (in the case of static)
Now looking back at the sentence of new, is it correct?
Customer C1 = new Customer (); step 11, summary: what is the meaning of this?
Because methods are unique and objects of all classes share these methods, JVM maintains a this in each class to find out who is calling a method. When the class is instantiated (that is, new), the this points to the object, and then when the method is called, implicitly pass in this, and you can get the data of the current real object in that method.
It's kind of like something, like a fancy name.
What is a flower name, for example, the property of the community will set up a flower name, they will declare to outsiders, the management of 1-10 buildings of the property housekeeper's name is Xiaofang, this Xiaofang is the flower name. This is a virtual name, and the owner only knows that the property housekeeper is Xiaofang, but Xiaofang may be Zhang San today and Li Si tomorrow. Zhang San, Li Si all left, and Wang Wu became the property butler. It is also said that he called the housekeeper Xiaofang.
Property setup housekeeper this position, do not know who will do it, so first set up, the housekeeper is called Xiaofang. Xiaofang is equivalent to this. Only when Zhang San takes office, this this will be Zhang San. Zhang San ran away, Li Si came, and this this is Li Si.
Step 12. I understand all the reasons, so what is static?
We have mentioned static more than once. What exactly is static? don't worry. Let's share it in the next section. Let's digest the knowledge of this section first.
Step 13, this section complete source code class Customer {String name; / / customer name String sex; / / gender String birthDate; / / birthday String phoneNumber; / / phone number int status / / customer status (1: normal, 2: abnormal) public Customer () {} void eat () {System.out.println (this.name + "eating...");}} class Vip {} class Demo {public static void main (String [] args) {Customer C1 = new Customer () Customer c2 = new Customer (); c1.name = "Arthur"; c2.name = "Angela"; c1.eat ();}}
Video explanation:
Java's understanding of this
Explanation of member method in Java
At this point, the study of "what is the java method and the principle of this keywords" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.