In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to understand the order of OO program execution in Java. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Before introducing the declaration and initialization order of Java, let's look at two classes: the Base class and the Derived class. Notice the whenAmISet member variable, and the method preProcess ()
Public class Base {Base () {preProcess ();} void preProcess () {}} public class Derived extends Base {public String whenAmISet = "set when declared"; @ Override void preProcess () {whenAmISet = "set in preProcess ()";}}
If we construct an instance of a subclass, what will be the value of whenAmISet?
Public class Main {public static void main (String [] args) {Derived d = new Derived (); System.out.println (d.whenAmISet);}}
Before moving on, please give yourself some time to think about the output of the above program. Yes, it does seem quite simple, and we should know the answer without even compiling and running the above code, so, do you think you know the answer? Are you sure your answer is correct?
Many people will think that the output of that program should be "set in preProcess ()". This is because when the constructor of the subclass Derived is called, it implicitly calls the constructor of its base class Base (through the super () function), so the constructor of the base class Base will call the preProcess () function, because the instance of this class is Derived, and the override keyword is used for this function in the subclass Derived. What is actually called is: Derived.preProcess (), and this method sets the value of the whenAmISet member variable to "set in preProcess ()".
Of course, the above conclusion is wrong. If you compile and run this program, you will find that the program actually outputs "set when declared". Why is that? Is the preProcess () method of the base class Base called? Neither is it! You can output something in the preProcess of the base class, and you will find that Base.preProcess () is not called when the program is running (otherwise it would be a catastrophic Bug for all Java applications).
Although the above conclusion is wrong, the derivation process is reasonable, but incomplete, and the following is the whole running process:
◆ enters the Derived constructor.
Memory for ◆ Derived member variables is allocated.
The ◆ Base constructor is implicitly called.
The ◆ Base constructor calls preProcess ().
The preProcess setting whenAmISet value for ◆ Derived is "set in preProcess ()".
The member variable initialization of ◆ Derived is called.
◆ executes the Derived constructor body.
Wait a minute. How is that possible? In step 6, the initialization of the Derived member comes after the call to preProcess ()? Yes, that's why we can't make the declaration and initialization of member variables an atomic operation, although we can write it together in Java to make it look like a declaration and initialization. But this is just an illusion, and our mistake is that we treat the declaration and initialization of Java as one. In C++ 's world, C++ does not support member variables to be initialized when declared, which requires you to explicitly initialize the value of their member variables in the constructor, which looks corny, but C++ actually has good intentions.
In the object-oriented world, because programs appear in the form of objects, we are confused about the order in which programs are executed. Therefore, in the object-oriented world, the order of program execution is very important.
The following is an one-by-one explanation of the above steps.
◆ enters the constructor.
◆ allocates memory for member variables.
◆ unless you explicitly call super (), Java secretly inserts super () at the front of the constructor of the subclass.
◆ calls the parent class constructor.
◆ calls preProcess, because the quilt subclass override, so the call is subclass.
◆ then initializes after preProcess (). This is because Java needs to ensure that the initialization of the parent class is earlier than the member initialization of the subclass, otherwise there will be problems using the member variables of the parent class in the subclass.
◆ formally executes the constructor of the subclass (this is an empty function, of course, but we don't declare it).
The above is how to understand the order in which OO programs are executed in Java. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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: 266
*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.