C# Intermediate Course-Polymorphism


Polymorphism is the ability to have multiple different manifestations or forms of the same behavior.
Polymorphism means multiple forms. In the object-oriented programming paradigm, polymorphism is often expressed as "one interface, multiple functions".
Polymorphism can be static or dynamic. In static polymorphism, the response of the function occurs at compile time. In dynamic polymorphism, the response of a function occurs at runtime.
In C#, every type is polymorphic because all types, including user-defined types, inherit from Object.

Static polymorphism

Function overloading

You can have multiple definitions of the same function name in the same scope. The definitions of functions must be different from each other, which can be different types of parameters in the parameter list, or different numbers of parameters. It is not possible to overload function declarations with different return types.

举例1:
using System;
namespace PolymorphismApplication
{
public class TestData
{
public int Add(int a, int b, int c)
{
return a + b + c;
}
public int Add(int a, int b)
{
return a + b;
}
}
class Program
{
static void Main(string[] args)
{
TestData dataClass = new TestData();
int add1 = dataClass.Add(1, 2);
int add2 = dataClass.Add(1, 2, 3);

        Console.WriteLine("add1 :" + add1);
        Console.WriteLine("add2 :" + add2);  
    }  
}  

}

Example 2:
using System;
namespace PolymorphismApplication
{ class Printdata { void print(int i) { Console.WriteLine("Output integer type: {0}", i ); }





  void print(double f)
  {
     Console.WriteLine("输出浮点型: {0}" , f);
  }

  void print(string s)
  {
     Console.WriteLine("输出字符串: {0}", s);
  }
  static void Main(string[] args)
  {
     Printdata p = new Printdata();
     // 调用 print 来打印整数
     p.print(1);
     // 调用 print 来打印浮点数
     p.print(1.23);
     // 调用 print 来打印字符串
     p.print("Hello Runoob");
     Console.ReadKey();
  }

}
}

Operator overloading

C# can redefine or overload the built-in operators in C#. Therefore, programmers can also use user-defined types of operators. Overloaded operators are functions with special names and are defined by the keyword operator followed by the symbol of the operator. Like other functions, overloaded operators have return types and parameter lists.
Example:
public static Box operator+ (Box b, Box c)
{ Box box = new Box(); box.length = b.length + c.length; box.breadth = b.breadth + c.breadth; box.height = b.height + c.height; return box; } The above function implements the addition operator (+) for the user-defined class Box. It adds the properties of two Box objects and returns the added Box object. In fact, I think it is better to use functions to implement operator overloading. Instead of: box3=box1+box2; it is better to box3=BoxAdd(box1,box2); the latter is more readable, you can directly look at the function implementation for easy modification.












Dynamic polymorphism

Dynamic polymorphism is achieved through abstract classes and virtual methods.

Guess you like

Origin blog.csdn.net/euphorias/article/details/104922118