C# Polymorphism
Polymorphism means having 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 function's response occurs at compile time. In dynamic polymorphism, the response of a function occurs at run time.
Static Polymorphism
At compile time, the connection mechanism of functions and objects is called early binding, also known as static binding. C# provides two techniques to implement static polymorphism. They are:
Function overloading
Operator overloading
Operator overloading will be discussed in the next chapter, and then we will discuss function overloading.
Function Overloading
You can have multiple definitions of the same function name in the same scope. Function definitions must differ from each other, either by the type of parameters in the parameter list or by the number of parameters. Function declarations that differ only in return type cannot be overloaded.
The following example demonstrates several identical function print(), used to print different data types:
using System; namespace PolymorphismApplication { class Printdata { void print(int i) { Console.WriteLine("Printing int: {0}", i ); } void print(double f) { Console.WriteLine("Printing float: {0}" , f); } void print(string s) { Console.WriteLine("Printing string: {0}", s); } static void Main(string[] args) { Printdata p = new Printdata(); // 调用 print 来打印整数 p.print(5); // 调用 print 来打印浮点数 p.print(500.263); // 调用 print 来打印字符串 p.print("Hello C++"); Console.ReadKey(); } } }
When the above code is compiled and executed, it will produce the following results:
Printing int: 5 Printing float: 500.263 Printing string: Hello C++
Dynamic polymorphism
C# allows you to use the keyword abstract to create abstract classes that provide implementations of partial classes of an interface. Implementation is complete when a derived class inherits from this abstract class. Abstract classes contain abstract methods, which can be implemented by derived classes. Derived classes have more specialized functionality.
Please note that here are some rules about abstract classes:
You cannot create an instance of an abstract class.
You cannot declare an abstract method outside an abstract class.
A class can be declared as sealed by placing the keyword sealed in front of the class definition. When a class is declared sealed, it cannot be inherited. Abstract classes cannot be declared sealed.
The following program demonstrates an abstract class:
using System; namespace PolymorphismApplication { abstract class Shape { public abstract int area(); } class Rectangle: Shape { private int length; private int width; public Rectangle( int a=0, int b=0) { length = a; width = b; } public override int area () { Console.WriteLine("Rectangle 类的面积:"); return (width * length); } } class RectangleTester { static void Main(string[] args) { Rectangle r = new Rectangle(10, 7); double a = r.area(); Console.WriteLine("面积: {0}",a); Console.ReadKey(); } } }
When the above code is compiled and executed, it will produce the following results:
Rectangle 类的面积: 面积: 70
When there is a function defined in the class that needs to be implemented in the inherited class, Virtual methods can be used. Virtual methods are declared using the keyword virtual. Virtual methods can have different implementations in different inherited classes. Calls to virtual methods occur at runtime.
Dynamic polymorphism is achieved through abstract classes and virtual methods.
The following program demonstrates this:
using System; namespace PolymorphismApplication { class Shape { protected int width, height; public Shape( int a=0, int b=0) { width = a; height = b; } public virtual int area() { Console.WriteLine("父类的面积:"); return 0; } } class Rectangle: Shape { public Rectangle( int a=0, int b=0): base(a, b) { } public override int area () { Console.WriteLine("Rectangle 类的面积:"); return (width * height); } } class Triangle: Shape { public Triangle(int a = 0, int b = 0): base(a, b) { } public override int area() { Console.WriteLine("Triangle 类的面积:"); return (width * height / 2); } } class Caller { public void CallArea(Shape sh) { int a; a = sh.area(); Console.WriteLine("面积: {0}", a); } } class Tester { static void Main(string[] args) { Caller c = new Caller(); Rectangle r = new Rectangle(10, 7); Triangle t = new Triangle(10, 5); c.CallArea(r); c.CallArea(t); Console.ReadKey(); } } }
When the above code is compiled and executed, it will produce the following results:
Rectangle 类的面积: 面积:70 Triangle 类的面积: 面积:25
The above is the content of [c# tutorial] C# polymorphism. For more related content, please Follow the PHP Chinese website (www.php.cn)!