Method overloading, hiding, rewriting and virtual method calls between subclasses and parent classes

Since the subclass object "collects" all the public methods of the parent class and the subclass at the same time, if a method in the subclass has the same signature as the parent class method (that is, the method name and method parameters are the same), then it should be accessed through the subclass object When using this method, is the method defined by the subclass or the parent class accessed?
In general, the relationship between subclass methods and parent class methods can be summarized into the following three types.

    Extend (Extend): Subclass method, parent class does not;

    Overload (Overload): The subclass has a function with the same name as the parent class, but the parameter types or numbers are different;

    Exactly the same: The subclass method is exactly the same as the parent class method from the method name to the parameter type.

For the first kind of "extended" relationship, since the method name of the subclass is different from that of the parent class, there is no problem of calling the method with the same name. Let's focus on the analysis of the latter two cases. 1. Overloading is the same as ordinary method overloading
.
The methods that constitute overloading in a class mainly decide which one to call based on the parameter list.
class Parent
{
  public void OverloadF()
  {
  }
}
class Child:Parent
{
  public void OverloadF(int i)
  {
  }
}
The usage code is as follows:
Child obj = new Child();
obj.OverloadF(); //Call parent class Overload method
obj.OverloadF(100);//Call the overload method of the subclass

It can be seen that although the overloaded methods are distributed in different classes, they can still be regarded as defined in the same class, and their usage is no different from calling other methods of the class.


2. Hide
class Parent
{
  public void HideF()
  {
    System.Console.WriteLine("Parent.HideF()");
  }
}
class Child : Parent
{
  public void HideF()
  {
    System.Console.WriteLine(" Child.HideF()"); //When the child class has exactly the same method as the parent class, it is said that "the child class hides the same name method of the parent class" } }Note: When Visual Studio compiles these two classes
  ,
it
will A warning is issued: 'HideExamples.Child.HideF()' hides the inherited member 'HideExamples.Parent.HideF()'. If hidden on purpose, use key new.


Child c = new Child();
c.HideF();//Call the method of the same name of the parent class or the subclass?
When the above code is running, the output is:
Child.HideF()
Modify the code:
Parent p = new Parent();
p.HideF();//Call the method with the same name of the parent class or the subclass?
The result of running the above code:
Parent.HideF()
From this, we can draw a conclusion:
when the two methods in the parent class and the child class are exactly the same, which method to call is determined by the type of the object variable.
However, the object-oriented inheritance feature allows subclass objects to be used as parent class objects, which complicates the problem. Please read the following code and think about what will happen?
Child c = new Child();
Parent p;
p = c;
p.HideF();//Call the method of the same name of the parent class or the subclass?
The running result of the above code is:
Parent.HideF()
This means that even though the Parent variable p actually refers to an object of Child type, the method called by p is still of the Parent class!
If you really want to call the method of the subclass, you should use it like this:
((Child)p).HideF();
That is, perform mandatory type conversion first.
Since the subclass hides the method of the same name of the parent class, if there is no mandatory conversion, the method of the same name of the subclass cannot be directly called through the variable of the parent class, even if the variable of the parent class refers to the object of the subclass.
Although the above warning does not affect the running results of the program, it tells us that the code does not conform to the grammatical specification of C#. Modify the definition of the Child class as follows: class Child : Parent {
public
new
void HideF()
{
System.Console.WriteLine("Child.HideF ()");
}
}
The "new" keyword clearly tells the C# compiler that the subclass hides the method of the same name of the parent class and provides its own new version.
Since the subclass hides the method with the same name of the parent class, if you want to call the hidden method of the same name in the parent class in the implementation code of the subclass method, please use the base keyword. The sample code is as follows:

base.HideF(); //Call the hidden method of the parent class


3. Override and virtual method calls
We hope that each object can only do what is within its own responsibilities, that is, if the parent class variable refers to the subclass object, the method defined by the subclass is called, and if The parent class variable refers to the parent class object, and the method defined by the parent class is called.
To achieve this purpose, you can add the keyword virtual before the method with the same name in the parent class, indicating that this is a virtual method, and the subclass can override this method: that is, add the keyword override before the method with the same name in the subclass, indicating that the method with the same name in the parent class was rewritten.
class Parent
{
  public virtual void OverrideF()
  {
    System.Console.WriteLine("Parent.OverrideF()");
  }
}
class Child : Parent
{
  public override void OverrideF()
  {
    System.Console.WriteLine("Child.OverrideF( )");
  }
}
Please see the following usage code:
Child c = new Child();
Parent p;
p = c;
p.OverrideF();//Call the method with the same name of the parent class or the child class?
The result of running the above code is: The example of
Child.OverrideF()
shows that the parent class method is defined as a virtual method, and after the subclass overrides the method with the same name, the method is called through the parent class variable. Whether to call the parent class or the subclass It is determined by the real object type referenced by the parent class variable, and has nothing to do with the parent class variable!
In other words, the same code:
p.OverrideF();
When p refers to different objects, the result of its operation may be completely different! Therefore, if we only program for the external interface provided by the parent class variable when programming, it will make our code a "chameleon", passing it different subclass objects (
these method with the same name), it does something different.
This is the "Virtual Method Invoke" feature of object-oriented languages.
Obviously, the "virtual method call" feature allows us to write very flexible code, greatly reducing the amount of code modification work caused by system function expansion and changes.
From this, the following conclusions are drawn:
the "virtual method call" feature of the object-oriented language allows us to use only the same statement to perform different operations according to the object type at runtime.

Guess you like

Origin blog.csdn.net/brightyanmin/article/details/51322966