java-super keyword

First look at a code:

insert image description here
Create a new Java class named Hero

insert image description here
and then create a new Java class named miao.

insert image description here
You can see this output:
insert image description here
at this time, this.name cannot see the name in its own class, but you can find it in the parent class through extends name.
But if a name: is added to the miao class,
insert image description here
there will be the following output:

insert image description here
indicating that the order of variable search is to find its own class first, and then find its own parent class.
The role of super in the code is as follows:
insert image description here
super: Indicates the content in the parent class.
this: Indicates the content in its own class.
Use super and this to distinguish the anonymous content in the parent class and the child class.
We define a subclass construction method miao, as follows:
insert image description here
Then, define a parent class construction method:
insert image description here
Then output:
insert image description here
You can see that the parent class is output first.
insert image description hereBut when outputting, the first output is the parent class.
insert image description here
In the first line of the subclass method, the parent class method will be called by default. It can only be placed on the first line, because the object of the parent class must be constructed first, and then go down.
super can get the content in the parent class
and can call the construction method in the parent class, which must be written in the first line of the subclass construction method. If the construction method of the parent class is parameterless, it is not required to write. If the parent class does not have a parameterless construction, it must be written.

Guess you like

Origin blog.csdn.net/weixin_55775980/article/details/127077423