The definition and difference of overload override and overlay in C

Time:2020-4-3

1. Heavy haul

1. Heavy load schematic

Overload means to reload. It refers to a method group with the same name in the same class, but with different parameter lists (return value can’t distinguish whether the function is overloaded or not). Overload has no keywords. Generally speaking, overloading means reloading functions or methods in the same class in memory.

2. Occurrence conditions

It must occur in a class. The same function name, different parameter types or numbers constitute an overload, regardless of the return type.

Such as:


Public class Name 
{ 
  Public string GetName(string username) 
  { 
    Return username; 
  } 
Public int GetName(int usercount) 
  { 
    Return usercount; 
  } 
}

Two, rewrite

1, hint

Override means that a subclass modifies the function body of a function in the parent class. The function in the parent class that needs to be overridden must be marked by virtual, abstract or override. Non virtual or static methods cannot be overridden.

Override is to inherit. If the function you write is the same as the function feature you want to inherit (function feature includes function name, parameter type and number), then, with this keyword, you can’t see the function of the parent class (or super class) when you use the function of this subclass. It’s overwritten.

2. Occurrence conditions

It must occur in the base class and derived class, the base class function must be decorated with virtual, abstract or override, and the derived class must be decorated with override.

Such as:

class Program 
  { 
    static void Main(string[] args) 
    { 
      oc oa=new ob(); 
      OA. B (1, 2); // the output is fdfdf 
    } 
    public class oc 
    { 
      private int a; 
     // encapsulation 
      public int geta 
      { 
        get { return a; } 
      } 
      public int seta 
      { 
        set { a = value; } 
      } 
     // function B 
      public virtual int b(int c, int e) 
      { 
        Console.WriteLine("{0}+{1}={2}", c, e, c + e); 
        return c + e; 
      } 
    } 
    //Derived class ob, base class OC 
    public class ob : oc 
    { 
    //Rewrite function B in base class 
      public override int b(int c,int e) 
      { 
        Console.WriteLine("fdfdf"); 
        return c; 
      }  
    } 
}

Three. Coverage

1, hint

Covering refers to redefining the function body with the same function characteristics as the parent class in the subclass. A function in a subclass has the same characteristics as a function in a parent class, but its body is different, which is called covering. This override, also known as hide, hides the same functions in the parent class and does not override them.

2. Occurrence conditions

Writing a non virtual function with the same name as the base class (with different parameters) in the subclass will hide the function in the base class. After compilation, you will be prompted to use the new keyword.

Such as:

class Program 
  { 
    static void Main(string[] args) 
    { 
      oc oa=new ob(); 
      OA. B (1, 2); // the output is 1 + 2 = 3 
    } 
    public class oc 
    { 
      private int a; 
    // encapsulation 
      public int geta 
      { 
        get { return a; } 
      } 
      public int seta 
      { 
        set { a = value; } 
      } 
    // function B 
      public int b(int c, int e) 
      { 
        Console.WriteLine("{0}+{1}={2}", c, e, c + e); 
        return c + e; 
      } 
    } 
  //Derived class ob, base class OC 
    public class ob : oc 
    { 
     //Override, hide function B in base class 
      public new int b(int c,int e) 
      { 
        Console.WriteLine("fdfdf"); 
        return c; 
      }  
    } 
}

The two functions of override have the same function characteristics. When they are applied to the subclass and the parent, they will override the parent function;

Although the function names of the two overloaded functions are the same, their features are different and they are applied in the same class;

The function names of the two functions of coverage (New) must be the same, but the function characteristics can be the same or different. When applied to the subclass and the parent class, the function with the same name of the parent class will be hidden.

summary

The above is the whole content of this article. I hope that the content of this article has some reference learning value for your study or work. Thank you for your support for developepaer. If you want to know more about it, please check the relevant links below