The difference between overloading and overriding

1. Overload: Simply put, the parameter list is different, the number is different/the same number but the type is different/number, the type is different. It can also be the same type but the order is different.

class Program
    {
        static void Main( string [] args) { int a = MyMath.Add( 12, 36 ); Console.WriteLine( " Integer addition result: {0} " ,a); float b = MyMath.Add( 12, 36.36 f ); Console.WriteLine( "The result of adding floating-point numbers: {0} " ,b); float ff = MyMath.Add( 36.36f, 23 ); Console.WriteLine( "The result of adding floating-point numbers and integers is: { 0} " ,ff); double d = MyMath.Add( 42.52, 23 ); Console.WriteLine( " The result of adding a double precision float and an integer is: {0} " ,d); } }
View Code

 

class MyMath
    {

        public static int Add(int a, int b)
        {
            return a + b;
        }

        public static float Add(float a, float f)
        {
            return a + f;
        }

        public static double Add(double a, double f)
        {
            return a + f;
        }

        public static decimal Add(decimal a, decimal f)
        {
            return a + f;
        }

        // The parameters are of the same type, but in a different order, constituting overloading. 
        public  static  float Add( int a, float f)
        {
            return a + f;
        }

        public static float Add(float f, int a)
        {
            return a + f;
        }
       // The type of parameters is the same, the number is different, and it constitutes overloading 
       public  static  int Add( int a)
        {
            return a;
        }

    }       
View Code

 2. Override: Override is to use override to modify methods, properties, indexers or events, and to override functions in base classes, in order to achieve polymorphism

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324650860&siteId=291194637