官术网_书友最值得收藏!

Polymorphism

The word polymorph means many forms. To understand the concept of polymorphism properly, let's work with an example. Let's think about a person, such as Bill Gates. We all know that Bill Gates is a great software developer, businessman, philanthropist, and also a great human being. He is one individual, but he has different roles and performs different tasks. This is polymorphism. When Bill Gates was developing software, he was playing the role of a software developer. He was thinking about the code he was writing. Later, when he became the CEO of Microsoft, he started managing people and thinking about growing the business. He's the same person, but with different roles and different responsibilities.

In C#, there are two kind of polymorphism: static polymorphism and dynamic polymorphism. Static polymorphism is a kind of polymorphism where the role of a method is determined at compilation time, whereas, in dynamic polymorphism, the role of a method is determined at runtime. Examples of static polymorphism include method overloading and operator overloading. Let's take a look at an example of method overloading:

public class Calculator {
public int AddNumbers(int firstNum, int secondNum){
return firstNum + secondNum;
}

public double AddNumbers(double firstNum, double secondNum){
return firstNum + secondNum;
}
}

Here, we can see that we have two methods with the same name, AddNumbers. Normally, we can't have two methods that have the same name; however, as the parameters of those methods are different, methods are allowed to have the same name by the compiler. Writing a method with the same name as another method, but with different parameters, is called method overloading. This is a kind of polymorphism.

Like method overloading, operator overloading is also a static polymorphism. Let's look at an example of operator overloading to demonstrate this:

public class MyCalc
{
public int a;
public int b;

public MyCalc(int a, int b)
{
this.a = a;
this.b = b;
}

public static MyCalc operator +(MyCalc a, MyCalc b)
{
return new MyCalc(a.a * 3 ,b.b * 3);
}
}

In the preceding example, we can see that the plus sign (+) is overloaded with another kind of calculation. So if you sum up two MyCalc objects, you will get an overloaded result instead of the normal sum, and this overloading happens at compile time, so it is static polymorphism.

Dynamic polymorphism refers to the use of the abstract class. When you write an abstract class, no instance can be created from that abstract class. When any other class uses or implements that abstract class, the class also has to implement the abstract methods of that abstract class. As different classes can implement the abstract class and can have different implementations of abstract methods, polymorphic behavior is achieved. In this case, we have methods with the same name but different implementations.

主站蜘蛛池模板: 寻甸| 团风县| 离岛区| 丹东市| 泰来县| 阜南县| 和龙市| 赤水市| 安图县| 云霄县| 定兴县| 壤塘县| 广昌县| 建水县| 盘山县| 平昌县| 苍南县| 武山县| 台州市| 衡东县| 青海省| 安义县| 大余县| 团风县| 洪湖市| 济阳县| 清流县| 安徽省| 建宁县| 桂林市| 伊宁市| 淮北市| 板桥市| 湾仔区| 浪卡子县| 瑞丽市| 永善县| 平度市| 阿巴嘎旗| 金沙县| 珠海市|