Polymorphism in C #
Polymorphism in C # can be static or dynamic. In particular, a static polymorph can be called a static polymorph, a response to a function defined at compile time and a dynamic polymorph can be called dynamic polymorphism, defined at runtime .
The word polymorphism means that there are many forms. In object-oriented programming, polymorphism is often expressed as "one Interface, multiple functions".
Polymorphism in C # can be static or dynamic . In particular, a static polymorph can be called a static polymorph, a response to a function defined at compile time and a dynamic polymorph can be called dynamic polymorphism, defined at runtime .
Static polymorphism in C #
The technique of linking a function to an object during compilation is called Early Binding. It is also called Static Binding. C # provides two techniques for deploying static polymorphisms, namely:
- Load overlap (Function overloading)
- Load operator stack (Operator overloading)
We have a separate discussion about operator overloading in C #.
Load overlap in C #
You can have multiple definitions for the same function name in the same scope. These definitions of the function must be different: such as type and / or number of parameters in the parameter list. In C #, you cannot overload function declarations but only in return types.
The following example illustrates how to use the print () function to print different data types in C #:
using System ; namespace VdNapChong { class InDuLieu { void print ( int i ) { Console . WriteLine ( "In số nguyên: {0}" , i ); } void print ( double f ) { Console . WriteLine ( "In số thập phân: {0}" , f ); } void print ( string s ) { Console . WriteLine ( "In chuỗi: {0}" , s ); } static void Main ( string [] args ) { InDuLieu p = new InDuLieu (); // Gọi hàm in số nguyên p . print ( 9 ); // Gọi hàm in số thập phân p . print ( 501.263 ); // Gọi hàm in chuỗi p . print ( "Học C# thật vui!" ); Console . ReadKey (); } } }
If you have read the previous C # articles, you will know, otherwise use the Console.ReadKey () command; then the program will run and finish (too fast to see the results in time). This command allows us to see the results more clearly.
Compiling and running the above C # program will produce the following results:
Print integer: 9
Print decimal number: 501.263
Chain printing: Learning C # is fun!
Dynamic polymorphism in C #
C # allows you to create abstract classes that are used to provide a local class implementation of an Interface. Implementation is completed when a derived class inherits from it. Abstract classes contain abstract methods implemented by derived classes. This derived class has more specialized functions.
Here are some rules about abstract classes in C #:
- You cannot create an Instance of an abstract class.
- You cannot declare an abstract method outside an abstract class.
- When a sealed class is sealed, it cannot be derived, abstract classes cannot be declared sealed.
The following example illustrates an abstract class in C #: create 3 classes named Shape, HinhChuNhat and TimDienTich respectively :
using System ; namespace VdLopAbstract {
// Lớp Shape là một lớp abstract abstract class Shape { public abstract int area (); } // Lớp HinhChuNhat là lớp dẫn xuất từ lớp Shape class HinhChuNhat : Shape { private int dai ; private int rong ; public HinhChuNhat ( int a = 0 , int b = 0 ) { dai = a ; rong = b ; } public override int area () { Console . WriteLine ( "Tính diện tích hình chữ nhật" ); return ( dai * rong ); } }
// Lớp TimDienTich chứa phương thức main() để thao tác trên HinhChuNhat class TimDienTich { static void Main ( string [] args ) { HinhChuNhat r = new HinhChuNhat ( 10 , 9 ); double a = r . area (); Console . WriteLine ( "Diện tích là: {0}" , a ); Console . ReadKey (); } } }
Compiling and running the above C # program will produce the following results:
Find the area of a rectangle
Area is: 90
When you have a function defined in a class that you want to deploy a derived class, you use the virtual function in C #. Virtual functions can be deployed in different ways in different inherited classes and calling these functions will be decided at runtime.
Dynamic polymorphism in C # is implemented by abstract classes and virtual functions.
The following example illustrates this: creating 5 classes named as follows, Image, Image, Timeline, HienThiDienTich and TimDienTich.
using System ; namespace VdLopAbstract {
// Lớp Hinh là lop abstract. class Hinh { protected int dai , rong ; public Hinh ( int a = 0 , int b = 0 ) { dai = a ; rong = b ; } public virtual int area () { Console . WriteLine ( "Diện tích của lớp cha là:" ); return 0 ; } }
// Lớp HinhChuNhat kế thừa từ lớp Hinh. class HinhChuNhat : Hinh { public HinhChuNhat ( int a = 0 , int b = 0 ): base ( a , b ) { } public override int area () { Console . WriteLine ( "Diện tích lớp HinhChuNhat là:" ); return ( dai * rong ); } }
// Lớp HinhTamGiac kế thừa từ lớp Hình class HinhTamGiac : Hinh { public HinhTamGiac ( int a = 0 , int b = 0 ): base ( a , b ) { } public override int area () { Console . WriteLine ( "Diện tích lớp HinhTamGiac là:" ); return ( dai * rong / 2 ); } }
// In dữ liệu diện tích ra màn hình. class HienThiDienTich { public void CallArea ( Hinh sh ) { int a ; a = sh . area (); Console . WriteLine ( "Diện tích: {0}" , a ); } }
// TimDienTich chứa main() thao tác với các đối tượng. class TimDienTich { static void Main ( string [] args ) { HienThiDienTich c = new HienThiDienTich (); HinhChuNhat r = new HinhChuNhat ( 10 , 7 ); HinhTamGiac t = new HinhTamGiac ( 10 , 5 ); c . CallArea ( r ); c . CallArea ( t ); Console . ReadKey (); } } }
Compiling and running the above C # program will produce the following results:
Class area of the image is:
Area: 70
The area of Hinh Tam's class is:
Area: 25
Follow tutorialspoint
Previous article: Calculating inheritance in C #
Next lesson: Operator overloading in C #
You should read it
- Learn about polymorphic malware and super polymorphism
- Object-oriented programming in Python
- Multiple Inheritance in Python
- Inheritance (Inheritance) in Python
- Static member of class in C ++
- Summary of trigonometric functions in Excel
- Complete financial functions in Excel you should know
- Instructions for creating interactive charts in Excel with INDEX function
- Recursive function in Python
- Comparison functions in Excel - How to use comparison functions and examples using comparison functions
- Be careful with polymorphic viruses
- PHP functions
Maybe you are interested
Concerned about security issues, SpaceX banned employees from using Zoom 10 reasons to be more generous with Microsoft 10 reasons threaten IE's hegemony 10 reasons to use iPad 3G instead of Wifi 1000 free music tracks to create Facebook, Instagram videos To protect your Facebook account in the most private way, read this article