A, overloading

1, heavy load schematic

“Overload” means to reload. It refers to a group of methods with the same name in the same class, but with different argument lists (the return value cannot distinguish whether the function is overloaded or not). Overload has no keyword. In colloquial terms, overloading means reloading a function or method from the same class in memory.

2. Occurrence conditions

Must occur in a class where the same function name and different parameter types or numbers constitute an overload, regardless of the return type.

Such as:


Public class Name  
{  
    Public stringGetName (stringUsername) {Return username; } PublicintGetName (intUsercount) {Return usercount; }}Copy the code

Second, the rewrite

1, the schematic

Override is when a subclass modifies the body of a function in its parent class. The function to be overridden must be annotated by virtual, abstract, or Override. It cannot Override non-virtual or static methods.

Override is an inheritance. If you write a function that has the same characteristics as the function that you are inheriting (including the name of the function, the types of the arguments, and the number of arguments), then add this keyword and you will not see the function of the parent class (or superclass) when you use the function of that subclass.

2. Occurrence conditions

Must occur in both base and derived classes. The base class function must be modified with virtual, abstract, or override, and the derived class 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;  
          / / packaging
            public int geta  
            {  
                get { returna; }}public int seta  
            {  
                set { a = value; }}B / / function
            public virtual int b(int c, int e)  
            {  
                Console.WriteLine("{0} + {1} = {2}", c, e, c + e);  
                returnc + e; }}// The derived class ob, the base class oc
        public class ob : oc  
        {  
        // Override function b in base class
            public override int b(int c,int e)  
            {  
                Console.WriteLine("fdfdf");  
                returnc; }}}Copy the code

Three, covering

1, the schematic

Overwriting refers to redefining the body of a function in a subclass that has the same function characteristics as the parent class. A function in the subclass has the same function characteristics as the function in the parent class, but the function body is different, called coverage. This overwriting, also known as hiding, hides the same function in the parent class without overwriting it.

2. Occurrence conditions

Writing a non-virtual function in a subclass with the same name as the base class (even if the arguments are different) will hide the function in the base class and prompt it to use the New keyword after compilation.

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;  
        / / packaging
            public int geta  
            {  
                get { returna; }}public int seta  
            {  
                set { a = value; }}B / / function
           public int b(int c, int e)  
            {  
                Console.WriteLine("{0} + {1} = {2}", c, e, c + e);  
                returnc + e; }}// The derived class ob, the 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");  
                returnc; }}}Copy the code

Two functions of Override have the same function characteristics. If applied to a subclass and a parent class, the function of the parent class is overridden. Although the two overloaded functions have the same name, they have different function characteristics and are applied to the same class. The name of the function that overrides (new) must be the same, but the characteristics of the function may or may not be the same. When applied to a subclass and a parent class, the function of the parent class with the same name is hidden.