In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the usage and details of the super keyword in Java. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
What are the advantages of Java. Simple, as long as understand the basic concepts, you can write applications suitable for a variety of situations; 2. Object oriented; 3. Distributed, Java is a network-oriented language; 4. Robust, java provides automatic garbage collection for memory management to prevent programmers from making mistakes when managing memory. ; 5. Security, Java used in the network and distributed environment must prevent the invasion of viruses. 6. Architecturally neutral, as long as the Java runtime system is installed, it can run on any processor. 7. Portability, Java can be easily ported to different machines on the network. 8. Interpretation execution, Java interpreter directly interprets and executes Java bytecode.
Preface
When reviewing the previous knowledge recently, I found that I didn't have a deep understanding of the Super keyword when I was learning object-oriented, so now I re-learn the usage and details of the super keyword.
1. The usage of the super keyword
There are three uses of the super keyword in Java, which are:
Through super. Property name to access member variables in the parent class
Through super. Method name (parameter list) to access member methods in the parent class
Access the constructor of the parent class through super (parameter list)
Call the member variable of the parent class:
Class Person {protected int age;} class Student extends Person {public void func () {int age = super.age; / / through super. The property name accesses the member variable of the parent class}} calls the member method in the parent class: class Person {protected void func () {}} class Student extends Person {public void func () {super.func (); / through super. The method name invokes the constructor of the parent class with the member method}} of the parent class: class Person {String name; public Person (String name) {this.name = name;}} class Student extends Person {public Student (String name) {super (name); / / call the constructor of the parent class through super (parameter list)}} Note:
Subclasses cannot access private-modified properties and methods in the parent class through the super keyword, because private-decorated properties and methods can only be accessed within the current class. When the constructor of the subclass invokes the constructor of the parent class through the super keyword, the super keyword must appear in the first line of the constructor and only once.
2. Details of using the super keyword
When we look for methods in subclasses, the invocation rules are as follows:
If there is a method in the subclass that needs to be called, just call it directly. If the method does not exist in the subclass, look up for the parent class. If the method exists in the parent class, then call the method in the parent class. If the method does not exist in the parent class, then look up for the parent class of the parent class until you find the Object class.
Tip: if the method is not found until the Object class, it will be prompted that the method does not exist, but an error will occur if the method is found up but does not have permission to access it (for example, it is decorated by private).
Public class Test extends Test2 {public static void main (String [] args) {Test test = new Test (); test.func1 ();} public void func1 () {System.out.println ("func1 () in subclass"); func2 ();}} class Test2 {public void func2 () {System.out.println ("func2 () in parent class") }} / / result: func1 () in the parent class of func2 () in the subclass
In the above code, the func1 () method in the subclass looks for the existence of the func2 () method in the subclass when calling the func2 () method, and looks for the func2 () method in its parent class if it is not found.
When we change the statement func2 (); in func1 () to this.func2 (); at this point, the semantics have not changed, we will still look in the subclass first, and if we don't find it, we will look in its parent class.
When we change this.func2 (); to super.func2 (); the semantics change, as mentioned above, super. The method name () calls the method in the parent class, so this statement does not detect whether the func2 () method exists in the current class, but only checks up in turn in its parent class. For example:
Public class Test extends Test2 {public static void main (String [] args) {Test test = new Test (); test.func1 ();} public void func1 () {System.out.println ("func1 () in subclass"); super.func2 (); / / compilation cannot pass} public void func2 () {System.out.println ("func2 () in word class") }} class Test2 {public void func3 () {System.out.println ("func3 () in parent class");}} / / failed compilation, indicating that the method func2 () in Test2 could not be parsed
Note: member attributes are the same as member methods.
As we already know, access to the super keyword is not limited to the parent class. Even the parent of the parent class, the parent of the parent class, and even further up, can be accessed through the super keyword. So, if the same member exists in multiple classes above the subclass, which class member is accessed using the super keyword?
When a subclass wants to access a member of a parent class, and multiple parent classes of the subclass have that member, we use the nearest principle for the use of the super keyword, that is, the first member found up by accessing the super keyword. For example:
Public class Test extends Test2 {public static void main (String [] args) {Test test = new Test (); test.func1 ();} public void func1 () {System.out.println ("func1 () in subclass"); super.func2 () / / the subclass accesses the func2 ()} class Test2 extends Test3 {public void func2 () {System.out.println ("func2 () in the parent class") in the parent class through the super keyword;}} class Test3 {public void func2 () {System.out.println ("func2 () in the parent class");}} / / result: func2 () in the func1 () parent class in the subclass
In the above code, the subclass accesses the func2 () method in the parent class through the super keyword, and the subclass inherits Test2,Test2 inherits Test3, and there is func2 () in both Test2 and Test3. In this case, the subclass first accesses the func2 () method in its parent class Test2,Test2, then you can directly access func2 () in Test2. If func2 () does not exist in Test2, the super keyword will continue to access.
3. Comparison of super and this keywords
The following table lists the differences between the super keyword and this keyword:
So much for sharing the usage and details of the super keyword in Java. I hope the above content can be of some help and can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.