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)05/31 Report--
In this article, the editor introduces in detail how to use the this and super keywords of Java. The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the this and super keywords of Java" can help you solve your doubts.
1. The use of the keyword "this" 1. Overview
What is this?
In Java, the this keyword is difficult to understand, and its function is very close to its meaning, meaning "present".
two。 Action
The this keyword can be used to modify and invoke: properties, methods, constructors.
The this keyword can only be used inside a method.
It is used inside the method, that is, a reference to the object to which the method belongs.
It is used inside the constructor to represent the object that the constructor is initializing.
3. Use
When do I use the this keyword?
This is used when the object that calls the method is needed within the method. Specific: we can use this to distinguish between properties and local variables. For example: this.name = name
3.1 modify properties and methods
This is understood as the current object or the object currently being created
In the methods of the class, we can use this. Property or this. Method, calling the current object property or method. However, in general, we choose to omit this. In special cases, if the formal parameter of the method has the same name as the property of the class, we must explicitly use this. Variable, indicating that the variable is an attribute, not a formal parameter.
When using this to access properties and methods, if it is not found in this class, it is looked for in the parent class.
Code demonstration:
Class Person {/ / defines the Person class private String name; private int age; public Person (String name,int age) {this.name = name; this.age = age;} public void getInfo () {System.out.println ("name:" + name); this.speak ();} public void speak () {System.out.println ("Age:" + this.age) }} 3.2 invoke the constructor
In the constructor of the class, we can use this. Property or this. Method, calling the object property or method that is currently being created. However, in general, we choose to omit this. In special cases, if the constructor parameter has the same name as the class property, we must explicitly use this. Variable, indicating that the variable is an attribute, not a formal parameter.
In the constructor of a class, we can explicitly use this (formal parameter list) to call other constructors specified in this class.
The constructor cannot call its own constructor through this (formal parameter list).
If there are n constructors in a class, at most n-1 constructors use this (formal parameter list)
Rule: this (formal parameter list) must be declared in the first line of the current constructor.
Within the constructor, at most one this (formal parameter list) can be declared, which can be used to call other constructors.
The compiler forbids calling a constructor in any method other than the constructor.
Code demonstration:
Class Person {/ / define Person class private String name; private int age; public Person () {/ / no-parameter constructor System.out.println ("new object instantiation");} public Person (String name) {this (); / / call non-parameter constructor this.name = name in this class;} public Person (String name,int age) {this (name) / / call the constructor this.age = age;} public String getInfo () {return "name:" + name + ", age:" + age;}} 3.3 to return the current object
Code demonstration:
Public class Leaf {int I = 0; Leaf increment () {ionization; return this;} void print () {System.out.println ("I =" + I);} public static void main (String args []) {Leaf x = new Leaf (); x.increment (). Print (); / / I = 3}} 2, the use of the "super" keyword 1. Overview
(1) super is understood as: parent class
(2) use super in the Java class to invoke the specified operation in the parent class:
Super can be used to access properties defined in the parent class.
Super can be used to call member methods defined in the parent class.
Super can be used to call the constructor of the parent class in the subclass constructor.
Especially when there is a member of the same name in the child parent class, you can use super to indicate that the member in the parent class is called, and the traceability of super is not limited to the direct parent class.
The usage of super is similar to this. This represents a reference to an object of this class, and super represents the identification of the memory space of the parent class.
two。 Use
The structure of the parent class can be called explicitly in the subclass.
3. Call properties and methods using 3.1
We can do it in the method or constructor of the subclass. Explicitly call a property or method declared in the parent class by using the super. Property or super. Method. However, in general, we are used to omitting "super."
When an attribute of the same name is defined in a subclass and a parent class, if we want to invoke the property declared in the parent class in the subclass, we must explicitly use the "super. Attribute" to indicate that the property declared in the parent class is called.
When a subclass overrides a method in the parent class, when we want to call the overridden method in the parent class in the method of the subclass, we must explicitly use the "super. Method" method, indicating that the method is called in the parent class.
3.2 invoke the constructor
(1) by default, all constructors in the subclass access the constructor of the hollow parameter of the parent class.
(2) when there is no constructor with null parameters in the parent class, the constructor of the subclass must specify to call the corresponding constructor in the class or the parent class through the this (parameter list) or super (parameter list) statement, otherwise the compilation will go wrong. At the same time, you can only "choose one of the two", cannot appear at the same time, and must be placed in the first line of the constructor.
(3) among the multiple constructors of a class, at least one class uses "super" in its constructor to call the constructor in the parent class.
Code demonstration:
Public class Person {private String name; private int age; private Date birthDate; public Person (String name, int age, Date d) {this.name = name; this.age = age; this.birthDate = d;} public Person (String name, int age) {this (name, age, null);} public Person (String name, Date d) {this (name, 30, d) } public Person (String name) {this (name, 30);}} public class Student extends Person {private String school; public Student (String name, int age, String s) {super (name, age); school = s;} public Student (String name, String s) {super (name); school = s } / / compilation error: no super (), the system will call the constructor of the parent class with no parameters. Public Student (String s) {school = s;}} III. The difference between this and super No. The thissuper1 access attribute accesses the attributes in this class. If the class does not have this attribute, continue to look for direct access to the attributes in the parent class. 2 call methods to access the methods in this class. If the class does not have this method, continue to find the method 3 invocation constructor in the parent class to directly access the parent class constructor. Call the parent class constructor on the first line of the constructor. Must be placed in the first line of the subclass constructor. 4. The whole process of subclass object instantiation
(1) from the point of view of the results: (inheritance)
After the child class inherits the parent class, it gets the properties or methods declared in the parent class.
Create an object of a subclass, and in the heap space, all properties declared in the parent class are loaded.
(2) from the process point of view:
When we create a subclass object through the constructor of the subclass, we must directly or indirectly call the constructor of the parent class, and then call the constructor of the parent class of the parent class. Until the constructor of the empty parameter in the java.lang.Object class is called. It is precisely because the structure of all the parent classes has been loaded that you can see that there is a structure in the parent class in memory, and the subclass object can consider making calls.
(3) clear: although the constructor of the parent class is called when creating the subclass object, an object has been created from beginning to end, that is, the subclass object of new.
After reading this, the article "how to use the this and super keywords of Java" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please 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.