heavy load , rewrite , Hiding these three concepts is not very clear to many people , I also missed a lot of information and asked the teacher to understand , If you have any shortcomings, please criticize and correct them !

  definition :

heavy load : Occurs in the same scope ( For example, in a class ), Define a series of methods with the same name , But the parameter list of the method is different . In this way, different parameters can be passed to decide which one to call . However, different return value types cannot constitute overload .

  rewrite : Occurs on inheritance , Redefining methods in a parent class in a child class , The methods in the subclass are the same as those in the parent . for example : The base class method is declared as virtual( Virtual method ), Used in derived classes
override Declare the override of this method .

  hide : Base class method is not declared ( Default to non virtual method ), Use in derived classes new Declare the hiding of this method .

  Example :

  heavy load :


  rewrite :


  hide :


  understand :

Overload must occur in a class , Same function name , Parameter type or number can be different , Return value types can be different . Select the calling method according to the parameters . Overloading is to let classes handle different data in a unified way , In the same class, multiple methods can use the same name, which is called method overload .

  rewrite override Generally used for interface implementation and method rewriting of inherited classes , it is to be noted that :

  1, The flag of the overridden method must match the flag of the overridden method exactly , To achieve the effect of coverage ;

  2, The return value of the overridden method must be the same as that of the overridden method ;

  3, The exception thrown by the overridden method must be the same as the exception thrown by the overridden method , Or its subclass ;

  4, The overridden method cannot be private, Otherwise, only a new method is defined in its subclass , It's not covered .

  5, so to speak ,override It's a very intelligent thing , It can dynamically decide whether to adopt the method of parent class or subclass .

Hiding is simply defined methods in the base class , Also needed in derived classes , If the two methods are identical, there will be syntax errors , So use keywords new Hide the methods in the base class , But it works when you want to use it , No grammar conflict .

©2019-2020 Toolsou All rights reserved,
java Four functional interfaces ( a key , simple )os Simple use of module HashMap Explain in detail html Writing about cherry trees , Writing about cherry trees It's unexpected Python Cherry tree (turtle The gorgeous style of Library ) computer network --- Basic concepts of computer network ( agreement , system ) Some East 14 Pay change 16 salary , Sincerity or routine ? Browser kernel ( understand )