C# Object-Oriented Polymorphism

Keywords: C#

1. Polymorphism

Polymorphism is a concept derived from inheritance that different derived classes behave differently in the same way as base classes. In the following examples, the swimming methods of animals are as follows:

 1 /// <summary>
 2 /// Animal
 3 /// </summary>
 4 public class Animal
 5 {
 6     public string Swimming()
 7     {
 8         return "Can't";
 9     }
10 }
11 /// <summary>
12 /// chicken
13 /// </summary>
14 public class Chicken : Animal
15 {
16 
17 }
18 /// <summary>
19 /// Dog
20 /// </summary>
21 public class Dog : Animal
22 {
23 }
24 /// <summary>
25 /// frog
26 /// </summary>
27 public class Frog : Animal
28 {
29 }

When users use derivative chicken-like swimming method, because the base class swimming method meets the needs of chickens, they will not return directly by calling the base class swimming method; when users use derivative dog and breast swimming method, because the realization of the base class swimming method does not meet the needs of dogs and frogs, dogs and frogs need to achieve self-realization. The logic of oneself. How to realize dog planing and breaststroke? The answer is rewriting (using the keywords virtual and override to re-implement the same name method of the base class in the derived class).

 1 public class Animal
 2 {
 3     //Design base classes using methods that need to be overridden in derived classes virtual Modify, use virtual The method of modification is called virtual method.
 4     public virtual string Swimming()
 5     {
 6         return "Can't";
 7     }
 8 }
 9 public class Dog : Animal
10 {
11     //Derived classes can override methods of base classes using override Modify the method with the same name as the base class, realize one's own behavior, be override Modifications can also be rewritten
12     public override string Swimming()
13     {
14         return "Dog paddle";
15     }
16 }
17 public class Frog : Animal
18 {
19     //Derived classes to override methods of base classes can use override Modify the method with the same name as the base class, realize one's own behavior, be override Modifications can also be rewritten
20     public override string Swimming()
21     {
22         return "Breaststroke";
23     }
24 }

In the example above, different derivatives (chickens, dogs, frogs) show different results on the swimming methods of the base class (animals), namely, the polymorphism of the class.

 

Another way to hide the polymorphism of classes is to use the new keyword to implement the different behavior of the derived classes with the same name method as the base class.

 1 public class Animal
 2 {
 3     public string Swimming()
 4     {
 5         return "Can't";
 6     }
 7 }
 8 public class Dog : Animal
 9 {
10     //Use new Keyword occlusion of the same name method of the base class can also be used in other members of the derived class
11     public new string Swimming()
12     {
13         return "Dog paddle";
14     }
15 }
16 public class Frog : Animal
17 {
18     //Use new Keyword occlusion of the same name method of the base class can also be used in other members of the derived class
19     public new string Swimming()
20     {
21         return "Breaststroke";
22     }
23 }

Note: The main use is when we do not modify the privileges of the base class and want to achieve the polymorphism of derived classes.

 

2. C# Keyword: base

The method of the derived class can be reused by using "base. base class method name".

 

C# Keyword: sealed

Because override-modified methods are implicitly rewritable, we can use the sealed keyword to prevent override when we do not want override-modified methods to be rewritten.

1 public class Dog : Animal
2 {
3     //Dog Class does not want its swimming methods to be rewritten by its derivatives
4     public sealed override string Swimming()
5     {
6         return "Dog paddle";
7     }
8 }

Note: In order to prevent override, the sealed keyword must exist the same as the override keyword.

 

IV. ABSTRACT CLASSES AND ABSTRACT METHODS

When the function of a base class is to provide public members for derived classes without any other practical significance, we do not want users to create this base class by using the new keyword. We can design the base class as an abstract class, so that users can not create it by using the new keyword. Using Abstract keywords to modify classes can make them Abstract classes.

When a method of an abstract class shows polymorphism in a derived class, the implementation of this method is useless for derivation. We hope that all derived classes must override this method of the base class. We can design this method as an abstract method so that the abstract method of the abstract class does not need to provide default implementations and derived classes. Override must be used to override this abstract method. If the derived class does not override the abstract method itself, it will become an abstract class. Using abstract keyword modifier can make it an abstract method.

 1 /// <summary>
 2 /// Electronic equipment with controllable temperature
 3 /// </summary>
 4 public abstract class TemperatureElectric
 5 {
 6     protected int temperature;
 7 
 8     public abstract int Up();
 9 
10     public abstract int Down();
11 }
12 /// <summary>
13 /// Air conditioner
14 /// </summary>
15 public class AirConditioner : TemperatureElectric
16 {
17     public override int Up()
18     {
19         if (temperature < 30)
20             temperature += 1;
21         return temperature;
22     }
23 
24     public override int Down()
25     {
26         if (temperature > 16)
27             temperature -= 1;
28         return temperature;
29     }
30 }
31 /// <summary>
32 /// Refrigerator
33 /// </summary>
34 public class Refrigerator : TemperatureElectric
35 {
36     /// <summary>
37     /// Increase refrigeration temperature
38     /// </summary>
39     public override int Up()
40     {
41         if (temperature < 7)
42             temperature += 1;
43         return temperature;
44     }
45 
46     /// <summary>
47     /// Decrease refrigeration temperature
48     /// </summary>
49     public override int Down()
50     {
51         if (temperature > 3)
52             temperature -= 1;
53         return temperature;
54     }
55 }

 

Contrast of Several Concepts

1. Overloading and Rewriting

The similarity between overloading and rewriting is that they all use the same name method.

The difference between overloading and rewriting is that the former is used in a single class, while the latter is used in two or more classes with inheritance relationship; the former is simplified in terms of usage intention, and the latter is extended in terms of function.

2. Rewriting and occlusion

The similarity between rewriting and occlusion is that they are both extensions to base class homonymy methods.

The difference between rewriting and occlusion is that through A = new B (); B inherits from A, the former calls the same name method of B, while the latter calls the same name method of A; the former is used for active design, and the latter is used for passive modification.

 1 public class RealizeObject
 2 {
 3     public void Realize()
 4     {
 5         //Dog It is through virtual Rewritten Animal
 6         Animal animal = new Dog();
 7         animal.Swimming();//Output Dog Planer
 8 
 9         //Dog It is through new Obscured Animal
10         Animal animal = new Dog();
11         animal.Swimming();//Output will not
12     }
13 }

3. Virtual and abstract methods

The similarity between virtual methods and abstract methods is that they can be rewritten.

The difference between virtual method and abstract method is that the former is more widely used than the latter, the former is used in non-sealed classes, the latter can only be used in abstract classes; the former must have an implementation part, the latter cannot have an implementation part; in derived classes, the former can not be overridden, and the latter must be overridden.

4. The difference between abstract class and interface type (see C# interface type)

Posted by ofaltins on Wed, 25 Sep 2019 05:45:19 -0700