当前位置:网站首页>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 .
边栏推荐
- Gradle knowledge points
- Pycaret successfully resolved the problem that 'sklearn model_ selection._ Search 'import name "\u check\u param\u grid"
- Why do we do autocorrelation analysis? Explain application scenarios and specific operations
- Mpai data science platform SVM support vector machine classification \ explanation of regression parameter adjustment
- PHP files running online
- The network traceroute command is used to determine the path through which IP packets access the destination address.
- Thinkphp3 use phpword to modify the template and download it
- Upgrade opsenssh to 8.8p1
- A commonly used statistical modeling method -- difference analysis
- R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用summary函数获取模型汇总统计信息
猜你喜欢

一款好用的印章设计工具 --(可转为ofd文件)

揭秘GaussDB(for Redis):全面對比Codis

The first techo day Tencent technology open day in 2022 will be held online on June 28
![[data midrange] what is the oneid of the data midrange? Isn't the master data fragrant?](/img/51/9aceaaeed1f6db7c60685d53477eba.png)
[data midrange] what is the oneid of the data midrange? Isn't the master data fragrant?

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

Happy shopkeeper source code -- Introduction to happy shopkeeper system development mode

什么是Flink?Flink能用来做什么?

Windows下MySQL的安装和删除

Explain factor analysis in simple terms, with case teaching (full)

An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down
随机推荐
Zunpin Yongyao advertising e-commerce system -- Zunpin Yongyao advertising e-commerce app system development source code sharing
If you also want to be we media, you might as well listen to Da Zhou's advice
VIM common commands and shortcut keys
Dark horse shopping mall ---2 Distributed file storage fastdfs
Arm V7 coprocessor
Polling and long polling
2022 Baidu collection batch automatic push assistant
An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down
How can we make an annual income of onemillion yuan by making our own media video?
ThinkPHP upload image compression size
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
2022 meisai e topic ideas sharing + translation
Some fields are ignored in tp6 query
揭秘GaussDB(for Redis):全面对比Codis
Tp6 exception handling try catch writing method
How to use ARIMA model for prediction?
19、wpf之事件转命令实现MVVM架构
一款好用的印章设计工具 --(可转为ofd文件)
Lighten the source code -- lighten the app system development function introduction to the beautiful world lighten the app system development source code in China
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用epiDisplay包的lrtest函数执行多个模型似然比检验对比两个模型的性能是否有差异