当前位置:网站首页>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 .
边栏推荐
- How can we differ LEFT OUTER JOIN vs Left Join [duplicate]
- High imitation blue playing network disk file sharing to make money network disk PHP system source code
- What is the primordial universe
- The network traceroute command is used to determine the path through which IP packets access the destination address.
- R语言使用构建有序多分类逻辑回归模型、epiDisplay包的ordinal.or.display函数获取有序logistic回归模型的汇总统计信息(变量对应的优势比及其置信区间、以及假设检验的p值)
- Pycaret successfully resolved the problem that 'sklearn model_ selection._ Search 'import name "\u check\u param\u grid"
- ECSHOP upload video_ ECSHOP video list, video classification, video related product guide
- Service charge and time setting code sharing involved in crmeb withdrawal process
- An example of using dynamic datalist
- Explanation of ideas and sharing of pre-processing procedures for 2021 US game D (with pre-processing data code)
猜你喜欢

【OceanBase】OceanBase简介及其与MySQL的比较

devsecops与devops的理解与建设

ECSHOP video list_ ECSHOP uploading video, video classification, video list playing video function

20. MVVM command binding of WPF

【数据中台】数据中台的OneID是个什么鬼,主数据它不香吗?

ECSHOP commodity page multi-attribute batch purchase plug-ins ECSHOP wholesale plug-ins multi-attribute order placing, multi-attribute batch purchase of commodities

What is the primordial universe

The source code of the hottest online disk money making system in 2022 imitates Lanzou online disk / Chengtong online disk / sharing money making cloud disk system / online disk VIP Download System

How to use SPSS to do grey correlation analysis? Quick grasp of hand-to-hand Teaching

揭秘GaussDB(for Redis):全面對比Codis
随机推荐
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用summary函数获取模型汇总统计信息
Mysql database logs binlog save aging (expire\u logs\u days)
刷入Magisk通用方法
Flutter common commands and problems
devsecops与devops的理解与建设
一款好用的印章设计工具 --(可转为ofd文件)
Online blind box system development function introduction and some source code sharing
An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down
[data midrange] what is the oneid of the data midrange? Isn't the master data fragrant?
什么是Flink?Flink能用来做什么?
2022 meisai topic C idea sharing + translation
An easy-to-use seal design tool - (can be converted to ofd file)
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、epiDisplay包的poisgof函数对拟合的泊松回归模型进行拟合优度检验(检验模型效果)
[regression analysis] understand ridge regression with case teaching
Thinkphp3 use phpword to modify the template and download it
Mpai data science platform random forest classification \ explanation of regression parameter adjustment
Installation and removal of MySQL under Windows
揭秘GaussDB(for Redis):全面對比Codis
ECSHOP quickly purchases goods, simplifies the shopping process, and improves the user experience through one-step shopping
Ubuntu uninstalling PHP