当前位置:网站首页>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 .
边栏推荐
- If you also want to be we media, you might as well listen to Da Zhou's advice
- 网络上开户买股票是否安全呢?
- An easy-to-use seal design tool - (can be converted to ofd file)
- Linear regression of common mathematical modeling models for College Students
- Heyangdao store management system -- share the development source code of heyangdao system
- 做自媒体视频需要怎么做才能年收入一百万?
- 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
- Zhangxiaobai's way of penetration (III) -- detailed explanation of SQL injection vulnerability principle (SQL Server)
- Today, I will explain to you what is DFI and its development prospects
- Concise H5 error page
猜你喜欢

Upgrade opsenssh to 8.8p1

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

19、wpf之事件转命令实现MVVM架构

ECSHOP video list_ ECSHOP uploading video, video classification, video list playing video function
![[on]learning dynamic and hierarchical traffic spatiotemporal features with transformer](/img/58/d68112a3d019de66150e2f5102f436.png)
[on]learning dynamic and hierarchical traffic spatiotemporal features with transformer

Uncover gaussdb (for redis): comprehensive comparison of CODIS

ECSHOP commodity page multi-attribute batch purchase plug-ins ECSHOP wholesale plug-ins multi-attribute order placing, multi-attribute batch purchase of commodities
![[oceanbase] Introduction to oceanbase and its comparison with MySQL](/img/1c/bd2bcddb7af4647407d2bc351f5f5d.png)
[oceanbase] Introduction to oceanbase and its comparison with MySQL
![[regression analysis] understand ridge regression with case teaching](/img/95/91f6f2a018b8d320945b12562492eb.jpg)
[regression analysis] understand ridge regression with case teaching

Explanation of ideas and sharing of pre-processing procedures for 2021 US game D (with pre-processing data code)
随机推荐
Today, I will explain to you what is DFI and its development prospects
New and old cluster migration of Minio data
ARM V7 连续加载/存储
Arm V7 coprocessor
Image tagging to obtain the coordinates of the image in the ImageView
20、wpf之MVVM命令绑定
How can we make an annual income of onemillion yuan by making our own media video?
When MySQL queries fields in JSON format, it takes a property value of JSON data
A set of automated paperless office system (oa+ approval process) source code: with data dictionary
Digital currency exchange -- digital currency exchange system development source code sharing
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用epiDisplay包的lrtest函数执行多个模型似然比检验对比两个模型的性能是否有差异
Learning notes 2022 overview | automatic graph machine learning, describing AGML methods, libraries and directions
19. Implementation of MVVM architecture based on WPF event to command
Concise H5 error page
Update of complex JSON in MySQL
Wait for the end of the network request in the uniapp Onshow method before executing the subsequent code content
Web project development process
Zunpin Yongyao advertising e-commerce system -- Zunpin Yongyao advertising e-commerce app system development source code sharing
Kotlin study notes
Mysql database logs binlog save aging (expire\u logs\u days)