Object-oriented C # - polymorphism

Three characteristics of object-oriented (encapsulation, inheritance, polymorphism) a large number of people should know. The first two words easier to understand. This article will mainly talk about object-oriented polymorphism.

 


What is polymorphism?
Different objects of the same operation, produce different execution result. This is polymorphism. That also contains the polymorphism: overloading, rewrite, virtual methods, abstract methods.

 

1, overloading
what overloaded?
The same function name in the same scope as the two or more methods, different parameter list of a method is called overloading. Overloading has three characteristics:
1.1: method name must be identical.
1.2: parameter list must be different.
1.3: Return Value may not be the same.

Column such as:

1 public void Animal()
2 {
3     Console.WriteLine("睡觉");
4 }
5 public void Animal(int time)
6 {
7     Console.WriteLine("{0}点睡觉", time);
8 }

 

2, rewrite
what is to rewrite? Subclass needs to satisfy their need to repeat the different implementations of a method defined in the base class. Rewrite need to use the override keyword. The method requires override base class methods must be virtual or abstract methods. Rewrite features:
2.1: the same method name.
2.2: The same argument list.
2.3: return the same value.

 

3, virtual methods
What is virtual?
The method allows override base class is called the subclass definition virtual methods. Use the virtual keyword definitions. Subclasses may not override the virtual method. Column such as:

. 1  class Animal
 2  {
 . 3          public  Virtual  void EatFood ()
 . 4          {
 . 5              Console.WriteLine ( " eat " );
 6          }
 7 }

Virtual methods can be called directly, such as:

1 Animal a = new Animal();
2 a.EatFood();

Output is:

Eat

Virtual method overridden by subclasses, such as:

. 1  class Bird: Animal
 2  {
 . 3          public  the override  void EatFood ()
 . 4          {
 . 5              Console.WriteLine ( " birds eating insects " );
 6          }
 7 }

 


4, abstract method
what is an abstract method?
And the method must be overridden in a subclass defined in the abstract base class called method. Use the keyword abstract definition. Note: The abstract method can only be defined in an abstract class.

 

The difference between abstract methods and virtual methods: as an abstract class can not be instantiated, abstract methods can not be invoked, that abstract methods can never be realized.

 

Scenario:
In fact, polymorphism is not very difficult to understand. Mainly how to utilize the actual development. So now I give an example: a large-scale factory now can produce Apple, Huawei, oppo phone, cell phone chip used in each is the same, the screen is not the same motherboard, Huawei phone has infrared. How would you achieve it?

. 1  public  abstract  class Phone
 2      {
 . 3          // phone chip 
. 4          public  String Chip { GET ; SET ;} = " Chip " ;
 . 5  
. 6          // phone screen 
. 7          public  String Screen { GET ; SET ;}
 . 8  
. 9          // motherboard 
10          public  String Mainboard { GET ; SET ;}
 . 11  
12 is          // production method of a mobile phone 
13         public  abstract  String Production ();
 14      }
 15  
16      // iPhone 
. 17      public  class Iphone: Phone
 18 is      {
 . 19          public  the override  String Production ()
 20 is          {
 21 is              Console.WriteLine ( " Start production iPhone " );
 22 is              return Chip + Screen + Mainboard;
 23 is          }
 24      }
 25  
26 is      // the OPPO 
27      public  classOppOphone: Phone
 28      {
 29          public  the override  String Production ()
 30          {
 31 is              Console.WriteLine ( " Start production OPPO phone " );
 32              return Chip + + Screen Mainboard;
 33 is          }
 34 is      }
 35  
36      // Huawei 
37 [      public  class HuaweiPhone: Phone
 38      {
 39          // Huawei phone has infrared 
40          public  String InfraredRay { GET ; the SET;}
 41 is  
42 is          public  the override  String Production ()
 43 is          {
 44 is              Console.WriteLine ( " Start production Huawei cell phone " );
 45              return Chip Mainboard + + + Screen InfraredRay;
 46 is          }
 47      }

So now produce apples and Huawei cell phone:

. 1          static  void the Main ( String [] args)
 2          {
 . 3              // production iPhone 
. 4              Iphone iPhone = new new Iphone () = {Screen " Apple screen " , Mainboard = " Apple Board " };
 . 5              Console.WriteLine (iphone.Production ());
 . 6  
. 7              // production Huawei cell phone 
. 8              HuaweiPhone HUAWEI = new new HuaweiPhone () = {screen " Huawei screen " , mainboard = " Huawei Board " , InfraredRay = "红外线" };
 9             Console.WriteLine(huawei.Production());
10             Console.ReadKey();
11         }

The implementation of the output:

Began production of Apple's mobile phone 
chip Apple Apple screen board 
began producing Huawei cell phone 
chip Huawei Huawei motherboard infrared screen

We can see iphone huawei objects and objects have implemented the same method Production produced different results. Remember the definition of polymorphism do? Different objects have different execution result of the same operation. Yes, this is polymorphism. Polymorphism effects: the different subclasses of the parent class object as a point of view, you can mask differences between different subclasses of objects, write common code to make common programming to adapt to changes in demand.

 

This article came to an end, if the wrong place but also look great God pointing.

Original from: C # object-oriented - polymorphism - had Yaping personal original blog

 

Guess you like

Origin www.cnblogs.com/zyp520/p/11992647.html