In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the relevant knowledge of dynamic case analysis in JAVA, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on dynamic case analysis in JAVA. Let's take a look.
/ / Let me explain the problem of dynamic implementation in Java with examples.
Class base
{
Public int ageOfBase=10
Public void display ()
{
System.out.println ("display () in base!")
}
Public void display1 ()
{
System.out.println ("display1 () in base!")
}
Private void show ()
{
System.out.println ("show () in base!")
}
}
Class test extends base
{
Public int ageOfTest=10
/ * this function overrides the parent method
But he doesn't simply complete his own function.
While realizing the function that he should achieve, he also retains the function of the parent class.
* *
The important thing is that if you don't override this parent class method,
There is still such a method in the subclass test, but there is a method.
It is actually inherited from the parent class, which simply implements the parent class.
The function that is realized.
, /
Public void display ()
{
System.out.println ("display () in test!")
Super.display ()
}
/ / notice that I did not overwrite the method display1 (), but this method is due to inheritance
/ / it still exists in the subclass test
/ / I write a new method as a subclass
Public void display2 ()
{
/ / the private method of the parent class is referenced here
/ / this method does not exist in the subclass
/ / but be sure to remember:
/ / access to private members of the parent class is not allowed
/ / super.show ()
/ / the common methods of the parent class are referenced here
/ / in fact, the reference here is a method in a subclass
/ / because this method has inherited from the parent class
Display1 ()
/ / it is OK to quote like this.
/ / here is the real reference to the method of the parent class
/ / super.display1 ()
/ / the above point may not be very clear
/ / Let me give you another example.
/ / this is a subclass.
Display ()
/ / this is the parent method
Super.display ()
System.out.println ("display2 () in test!")
}
Public static void main (String args [])
{
Test t=new test ()
Base b=new base ()
/ / the following sentence is the root of polymorphism
/ / A reference to a parent class points to a subclass
Bimt
/ / if the method is called through the reference of the parent class
/ / actually executes the methods implemented in the subclass
/ *
Next, I would like to talk about my experience and views based on my mastery and research on object-oriented programming.
In the meantime, I can't guarantee that everything I said is correct, just for your reference.
Polymorphism is an important concept in the field of object-oriented programming. I combine C++ and java
Make a few explanations about the actual situation.
(1) it is best to have some knowledge of virtual pointers and virtual pointer tables.
If you don't understand, you can only remember what I said, but you may not understand it very well.
(2) the parent class reference points to the subclass. When the method is called, the method of the subclass is executed.
There are many premises and accidents in this argument.
Let me give you an example.
, /
B.display ()
/ / * there is a mistake in the original article *
/ / the following call will make an error ()
/ / t.display2 ()
/ / * end of error in original expression *
/ / now it should be changed to the following description
/ / the following line should be changed to b.display ()
/ / only in this way can we combine the examples given in this article. At the same time, the following line can be run.
/ / the author solemnly apologizes for such a writing mistake.
/ / *
/ / it should be like the following
((test) b) .display2 ()
Int temp1=b.ageOfBase
/ / the following call will make an error
/ / int temp2=t.ageOfTest
/ / it should be like the following
Int temp2= ((test) b) .ageOfTest
/ *
Now explain the above two reasons why things can go wrong, which is the root of dynamics.
I tried to explain a little more clearly.
* key one *
Because the objects of the parent class and the subclass have different sizes in memory, of course.
It is impossible for different objects to have the same memory address.
There should be no problem with this, of course, if the subclass is only simple
If you inherit and don't do anything, it may be the same, but in C++
There will be a little difference among compilers, and it may also be different.
* key two *
References are essentially implemented through pointers, and a deeper point is that references are pointers to pointers.
The memory space managed by different types of pointers is different, which is the same as above.
It's a tight connection. Note that here we are talking about pointer-managed memory rather than pointers. I think everyone
Knowing that the pointer is only 4 bytes long. Here we can simply say that the visibility of variables
Problem, because variables exist but are not necessarily visible.
* key three *
The key to polymorphism is dynamic binding. To put it bluntly, it is virtual function virtual pointer virtual pointer table.
Wait for several important concepts. In a nutshell, it's complicated, but every real
Object-oriented programmers must take the time to understand this well. There are only static functions in java
And virtual functions, C++ also has a non-static non-virtual function. So the dynamics of java
Binding is easier. You can understand it this way inaccurately (because I am also lazy to explain accurately.
You can write a long article), each time the class is compiled, a virtual function table is generated.
The table holds all the virtual function addresses in its own class, and if the class has a parent class
(whether it's extends class or implements interface), at the same time, he will put
The virtual function table inherited from the parent class is also inherited (I'm just trying to explain it here, not very accurately).
Please pay attention to this place:
* * *
If the subclass does not override the method inherited by the parent class, the virtual function address in the parent virtual function
Is changed into the subclass's own virtual function address; if there is no change, there is no need to change the inherited
The virtual function table corresponds to the address of the function.
* * *
* start to explain *
When the parent class reference points to the subclass object, the parent class references the managed memory space
It is smaller than the real memory space of the subclass. So there are more subclasses than in the parent class.
Things are not visible to this parent class reference. So there is no way to quote it.
This is true of the variable reference and function reference above, because there is no
Variable ageOfTest and function display2 (). But we can know the parent pointer.
It actually points to subclasses, so our solution to this problem is:
Cast type (called modeling in java).
At the same time, someone may ask, isn't that function address in the subclass also in the virtual function table?
I'm glad you asked such a question, but the virtual function table seen by the parent class reference is not
Of that function project, because it is absolutely invisible to the parent class reference.
For another virtual function call, if you read the instructions very carefully, you must now
We know that although the memory range of the parent class reference management is small, the virtual function table is available in the parent class.
But because the function is rewritten in the subclass, the function address programming subclass in that table pulls
Of course, it is the method that refers to the subclass.
/ * important supplement *
Static functions in java are not dynamic, because static functions cannot be inherited and cannot be
Rewrite to a non-static method. Because static means that all instances of the class share a single entity.
* * *
, /
}
}
This is the end of the article on "dynamic case Analysis in JAVA". Thank you for reading! I believe you all have a certain understanding of the knowledge of "dynamic case Analysis in JAVA". If you want to learn more knowledge, 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: 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.