当前位置:网站首页>The difference between this and super and their respective functions
The difference between this and super and their respective functions
2022-06-25 12:33:00 【mercenary's L】
1. Attribute distinction :
this Access the properties in this class , If this class does not have this property, continue to search from the parent class .super Access properties in the parent class .
2. Differences in methods :
this Access the methods in this class , If this class does not have this method, continue to search from the parent class .super Access methods in the parent class .
3. Differences in structure :
this Call this class to construct , Must be placed on the first line of the constructor .super Call the parent class construct , Must be placed on the first line of the subclass constructor .
4. Other differences :
this Represents the current object .super Cannot represent the current object
A、this. Variables and super. Variable
this. Variable The variable of the current object ;
super. Variable The variables in the parent class are called directly .
B、this( Parameters ) and super( Parameters ) Method
this( Parameters ) call ( forward ) Is the constructor in the current class ;
super( Parameters ) Used to confirm which constructor in the parent class to use .
Two 、 Be careful :
1) When initializing a subclass that has a parent class , The constructor of the parent class also executes , And takes precedence over the constructor of the subclass ; Because the first line in the constructor of each subclass has a default implicit statement super();
2)this() and super() Can only be written on the first line of the constructor ;
3)this() and super() Cannot exist in the same constructor . First of all ,this() and super() Must be written on the first line of the constructor ; second ,this() Statement calls another constructor of the current class, and there must be a constructor of the parent class in this other constructor , Reuse super() Call the constructor of the parent class again , It is equivalent to calling the constructor of the parent class twice , The compiler will not pass ;
4)this and super Cannot be used for static Decorated variable , Method , Code block ; because this and super All refer to the object ( example )
One 、this
this It's an object of its own , Represents the object itself , It can be understood as : A pointer to the object itself .
this The usage of is in java In general, it can be divided into 3 Kind of :
1、 A common direct quote
this It points to the current object itself .
2、 Parameter and member name are the same , use this To distinguish between :
public Person(String name, int age) {
this.name = name;
this.age = age;
}3. Reference the constructor of this class
class Person{
private String name;
private int age;
public Person() {
}
public Person(String name) {
this.name = name;
}
public Person(String name, int age) {
this(name);
this.age = age;
}
}Two 、super
super Can be understood as pointing to their own super ( Father ) A pointer to a class object , And this superclass refers to the nearest parent class .
super There are also three uses :
1、 A common direct quote
And this similar ,super Equivalent to a reference to the parent of the current object , In this way, you can use super.xxx To reference the members of the parent class .
2、 When a member variable or method in a subclass has the same name as a member variable or method in a parent class , use super Distinguish
class Person{
protected String name;
public Person(String name) {
this.name = name;
}
}
class Student extends Person{
private String name;
public Student(String name, String name1) {
super(name);
this.name = name1;
}
public void getInfo(){
System.out.println(this.name); //Child
System.out.println(super.name); //Father
}
}
public class Test {
public static void main(String[] args) {
Student s1 = new Student("Father","Child");
s1.getInfo();
}
}3、 Reference parent constructor
super( Parameters ): Call one of the constructors in the parent class ( Should be the first statement in the constructor ).
this( Parameters ): Call another form of constructor in this class ( Should be the first statement in the constructor ).
3、 ... and 、super and this Similarities and differences :
- super: It references members in the immediate parent class of the current object ( It is used to access member data or functions hidden in the direct parent class , When the base class and the derived class have the same member definition, such as :super. Variable name super. Member function by name ( Actual parameters )
- this: It represents the current object name ( It is easy to produce ambiguity in the procedure , You should use this To indicate the current object ; If the parameter of a function has the same name as the member data in the class , In this case, it is necessary to this To indicate the member variable name )
- super() and this() similar , The difference is that ,super() The method of calling the parent class in the subclass ,this() Call other construction methods of this class in this class .
- super() and this() All need to be placed in the first line of the construction method .
- Although it can be used this Call a constructor , But you can't call two .
- this and super Cannot appear in a constructor at the same time , because this It's bound to call other constructors , Other constructors must have super The existence of sentences , So there are the same statements in the same constructor , You lose the meaning of the sentence , The compiler won't pass either .
- this() and super() It's all about the object , therefore , None of them can be in static Use in the environment . Include :static Variable ,static Method ,static Sentence block .
- essentially ,this Is a pointer to this object , However super It's a Java keyword .
边栏推荐
- PHP parsing QR code content
- Understanding and construction of devsecops and Devops
- Controllable character image synthesis based on attribute decomposition and Gan reproduction
- Flutter common commands and problems
- Zunpin Yongyao advertising e-commerce system -- Zunpin Yongyao advertising e-commerce app system development source code sharing
- Zhengzheng e-commerce source code -- Zhengzheng advertising e-commerce system development source code sharing
- PHP multidimensional array sorting
- 2022 meisai topic C idea sharing + translation
- 2022 meisai e topic ideas sharing + translation
- Dark horse shopping mall ---3 Commodity management
猜你喜欢

Dark horse shopping mall ---2 Distributed file storage fastdfs

Service charge and time setting code sharing involved in crmeb withdrawal process
![[regression analysis] understand ridge regression with case teaching](/img/95/91f6f2a018b8d320945b12562492eb.jpg)
[regression analysis] understand ridge regression with case teaching

Linear regression of common mathematical modeling models for College Students

Dark horse shopping mall ---1 Project introduction - environment construction

15、wpf之button样式小记

The server reported an error 503 service unavailable:the system returned: (71) protocol error

An easy-to-use seal design tool - (can be converted to ofd file)

Explain AHP in human language (very detailed principle + simple tool implementation)

一套自动化无纸办公系统(OA+审批流)源码:带数据字典
随机推荐
15、wpf之button样式小记
Huile optimization system -- sharing of secondary development source code of huile optimization app system
ECSHOP whole site custom URL supports directory type
JQ dynamic setting radio does not take effect when selected
揭秘GaussDB(for Redis):全面對比Codis
Image tagging to obtain the coordinates of the image in the ImageView
Wait for the end of the network request in the uniapp Onshow method before executing the subsequent code content
Zhengzheng e-commerce source code -- Zhengzheng advertising e-commerce system development source code sharing
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用epiDisplay包的lrtest函数执行多个模型似然比检验对比两个模型的性能是否有差异
ECSHOP video list_ ECSHOP uploading video, video classification, video list playing video function
The R language uses the follow up The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The type parameter specifies the line type of
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用summary函数获取模型汇总统计信息
Network | indicators and test methods to measure the quality of the network
实现领域驱动设计 - 使用ABP框架 - 系列文章汇总
R语言使用构建有序多分类逻辑回归模型、epiDisplay包的ordinal.or.display函数获取有序logistic回归模型的汇总统计信息(变量对应的优势比及其置信区间、以及假设检验的p值)
ARM V7 协处理器
Dark horse shopping mall ---6 Brand, specification statistics, condition filtering, paging sorting, highlighting
Arm V7 coprocessor
apple 为什么要改 objc_msgSend 的类型申明
R language uses ordinal or. The display function obtains the summary statistical information of the ordered logistic regression model (the odds ratio and its confidence interval corresponding to the v