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

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.

主站蜘蛛池模板: 茂名市| 贵阳市| 吴川市| 德格县| 莲花县| 临猗县| 晋城| 荣昌县| 临沂市| 花莲市| 乌兰县| 曲周县| 神池县| 沙洋县| 甘泉县| 宜章县| 仙居县| 普兰县| 江源县| 扶绥县| 玉山县| 石阡县| 修水县| 滕州市| 营山县| 襄汾县| 吉首市| 禄丰县| 延川县| 类乌齐县| 陆川县| 林州市| 韶关市| 洱源县| 丹棱县| 双柏县| 绍兴市| 遵化市| 基隆市| 静海县| 乃东县|