keyword super

Use the keyword super to refer to member variables or methods of the parent class that are hidden by the child class. The format of the statement referenced by super is:

super. member variable name;

super. method name (argument list);

super refers to the parent class object, and the main usage forms are as follows.

1. Reference the constructor of the parent class inside the constructor of the subclass

When constructing an object of a subclass, the constructor of the superclass must be called. In order to facilitate the writing of the code, the default constructor in the parent class is automatically called inside the constructor of the subclass. But if there is no default constructor in the parent class, it must be called manually.

Using super can call the dog's paw method of the parent class inside the constructor of the child class. The constructor in the parent class can be called as needed inside the constructor of the subclass.

2. Call the member method in the parent class in the subclass

Inheriting member methods in the parent class in the subclass can generally be used directly through the method name, but if the member method of the parent class is overridden in the subclass, if you need to call the overridden member method in the parent class inside the subclass can not be called directly, so you need to use the super keyword again.

3. Call the member variable in the parent class in the subclass

In the subclass, if you refer to the member variable of the parent class, you can also use "super.member variable" to refer to it, but it is meaningless to cover the general member variable. You can directly use the member variable name for reference at this time, so here The super can be omitted.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326481963&siteId=291194637