【JAVA SE】The use and difference of "super" and "this"

Kind tips

Hello everyone, I am Cbiltps. In my blog, if there are sentences that are difficult to understand or key points that are difficult to express in words , I will have pictures . So my blog with pictures is very important ! ! !

The knowledge points in this section are extracted from my main blog . The main blog actually serves as an expanded context to reflect the logical order for all knowledge points . Therefore, I will pull out the things that are worth discussing in it separately, so that everyone can eat them!

Everyone is welcome to correct me, come on!

start of text

In the previous study, I have encountered the two keywords super and this . The blogger made the following summary based on what he has learned and blog references :

super: It can be understood as a reference to the parent class object ( which is dependent on the object ), and cannot appear in the static environment (including: static variables, static methods, static statement blocks) ( because static does not depend on objects )!

  • super(); //调用父类的构造方法
  • super.func(); //调用父类的普通方法
  • super.data; //调用父类的成员属性

this: It can be understood as a pointer to this object, which represents the name of the current object (where ambiguity is likely to occur in the program, this should be used to indicate the current object ; if the member data in the formal participation, this need to use this to specify the member variable name )!

  • this(); //调用本类中另一种形成的构造方法

Note and difference summary:

  • super();The difference this();is: super();call the constructor of the parent class from the subclass, and this();call other methods in the same class
  • super();and both need to be placed in the first linethis(); of the constructor
  • Sometimes thisand supercannot appear in a constructor at the same time , because this will inevitably call other constructors, and other constructors must also have super statements, so if there are the same statements in the same constructor, it will be lost. statement meaning, the compiler will not pass
  • this();and super();both refer to objects , so neither can be used static环境in (including: static variable, static method, static statement block)

insert image description here

Guess you like

Origin blog.csdn.net/Cbiltps/article/details/122514697