First look at a code:
Create a new Java class named Hero
and then create a new Java class named miao.
You can see this output:
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,
there will be the following output:
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:
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:
Then, define a parent class construction method:
Then output:
You can see that the parent class is output first.
But when outputting, the first output is the parent class.
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.
java-super keyword
Guess you like
Origin blog.csdn.net/weixin_55775980/article/details/127077423
Recommended
Ranking