Home > Back-end >  About the parent class static method in a subclass to be rewritten.
About the parent class static method in a subclass to be rewritten.

Time:10-26

So, yesterday, to solve the problem. See a problem,
As shown in figure,
I personally is biased towards the static options
But I still to use the code to verify the,
As shown: use override test is not through, on behalf of the failed rewritten,
Later, I dropped the override annotation, in actual operation,
At first using a subclass object to call the subclass method, the result is the parent class method,
I then use the superclass object to call subclasses method, the result is the parent class instead be rewritten by inherited methods, some say it has not been essentially rewritten,

Dear bosses, who can explain not, what is this situation



CodePudding user response:

The concept is not clear,
Inheritance, coverage, rewrite these is object oriented, according to the concept of "object" in terms of,

Static methods, specifically, do not belong to the content of the object oriented, there is no inheritance, cover, such as

If the parent and child classes, they are written the same name of static method,
So called, it is recommended to use "of" the name of the class. The method name to distinguish which method to invoke, and an example is used to call is not in conformity with the coding standards,
Your IDEA, in the zi. M () this line, also displays a warning:
Access the static 'Zi. M () via the class' Zi 'reference
It means that, you should go through the Zi class reference to call, should not be used examples to call,

CodePudding user response:

If if, you must be in violation of the coding standards, must use the instance to invoke the static method,
You define an instance of the, if is the parent class type, then perform a static method of the parent, if the subclass type, then perform a subclass of static methods, such as:
 
Fu Fu=new Fu ();
Fu. M ();//execution Fu class static methods

Fu=new Zi ();
Fu. M ();//execution Fu class static methods

Fu Zi Zi=((Zi));
Zi. M ();//execution Zi static method of a class

CodePudding user response:

A static method is a class level, not be about object, if the object is used to call a static method, the object. GetClass () is what class, what is called a static method of a class,
If not rewritten the static method, that is, objects. GetClass () class can not find a static method, will continue to find its parent class, until find a static method, so subclasses override the static method, use object to call would be a subclass of static method,

CodePudding user response:

refer to the building up network response:
, yesterday, to solve the problem. See a problem,
As shown in figure,
I personally is biased towards the static options
But I still to use the code to verify the,
As shown: use override test is not through, on behalf of the failed rewritten,
Later, I dropped the override annotation, in actual operation,
At first using a subclass object to call the subclass method, the result is the parent class method,
I then use the superclass object to call subclasses method, the result is the parent class instead be rewritten by inherited methods, some say it has not been essentially rewritten,

Dear bosses, who can explain not, what is this situation


A static method cannot be covered, can only use static method to hide the parent class method with the same, this is the basic knowledge,

CodePudding user response:

The building Lord ah, as a CSDNer, post code level needs to improve!
Do you have pictures of kung fu, it is better to copy and format paste directly to the BBS,,,
  • Related