A polymorphic
Polymorphism is lead out from the concept of inheritance, i.e. different derived classes to the same base class exhibit different behavior. As an animal swimming in the following example methods:
1 /// <summary> 2 /// 动物 3 /// </summary> 4 public class Animal 5 { 6 public string Swimming() 7 { 8 return "不会"; 9 } 10 } 11 /// <summary> 12 /// 鸡 13 /// </summary> 14 public class Chicken : Animal 15 { 16 17 } 18 /// <summary> 19 /// 狗 20 /// </summary> 21 public class Dog : Animal 22 { 23 } 24 /// <summary> 25 /// 蛙 26 /// </summary> 27 public class Frog : Animal 28 { 29 }
When a user using methods derived class swimming chicken, because the swim base class methods to meet the needs of the chicken, then swim directly call the base class method will not return; when a user uses a derived class dogs and frogs swimming method, since the base class swimming implemented method does not meet the needs of dogs and frogs, so dogs and frogs need to implement their own logic. How to dog paddle dog and frog breaststroke? The answer is to rewrite (using the keyword virtual re-implementation of the same name and override the base class in a derived class).
. 1 public class Animal 2 { . 3 // use the virtual modification of the method needs to be rewritten in a derived class design the base class virtual using a modified method is called virtual methods . 4 public virtual String Swimming () . 5 { . 6 return " not " ; . 7 } . 8 } . 9 public class Dog: Animal 10 { . 11 // derived class to overwrite the base class can be used to override a modified method of the base class of the same name, to achieve their behavior, the method may also be modified override is rewritten 12 is public the override String Swimming () 13 is { 14 return " dog paddle " ; 15 } 16 } . 17 public class Frog: Animal 18 is { . 19 // derived class to override methods in the base class, the base class method override modifying the same name may be used to achieve their behavior, be override the modified method may also be rewritten 20 is public the override String Swimming () 21 is { 22 is return " breaststroke " ; 23 } 24 }
In the above example, the different derived classes (chicken, dogs, frogs) to the base class (animal) Swimming method exhibit their different results, i.e., the polymorphic properties of the class.
It may also be another way to achieve polymorphism shielding characteristics such that the derived class to implement the new keyword with different behaviors of the base class methods of the same name.
. 1 public class Animal 2 { . 3 public String Swimming () . 4 { . 5 return " not " ; . 6 } . 7 } . 8 public class Dog: Animal . 9 { 10 // new keyword occlusion methods of the base class of the same name may be used to the other members of the derived class . 11 public new new String Swimming () 12 is { 13 is return " dog paddle " ; 14 } 15 } 16 public class Frog: Animal . 17 { 18 is // same method on the new keyword shutter base class may be used to other members of the derived class . 19 public new String Swimming () 20 is { 21 is return " breaststroke " ; 22 } 23 }
Note: The main use case is that when we do not have permission to modify the base class and want to implement multi-state properties of the derived class.
Two, C # Keywords: base
Use "Base. The method of the base class name" in the process of a derived class of the base class can be reused.
Three, C # Keywords: sealed
Since the modified override method is implicitly rewritable, so when we do not want to override the modified method is overridden, you can use the sealed keyword to prevent being overwritten.
. 1 public class Dog: Animal 2 { . 3 // Dog class does not want their swim method is overridden its derived classes . 4 public Sealed the override String Swimming () . 5 { . 6 return " dog paddle " ; 7 } 8 }
Note: to prevent overwriting was, sealed keyword must be identical to the presence of the override keyword.
Four, abstract classes and abstract methods
When the role of a base class only to provide members of the public to the derived class, when no other practical sense, we do not want users to create new keyword by this base class, the base class can be designed as an abstract class, so the user can not use new key word to create it. Use the keyword abstract class can be modified so that it becomes an abstract class.
When an abstract class method showed polymorphism in a derived class, the derived implementation of this method is useless, we hope that this method of the base class for all derived classes must override this method may be designed to abstract methods, abstract methods abstract class so do not provide default implementations and derived class must override this method abstract rewrite. If the derived class does not override the abstract method itself will also become an abstract class. Use the keyword abstract method can be modified so that it becomes abstract methods.
. 1 /// <Summary> 2 /// schedulable temperature electronic devices . 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 is /// <Summary> 13 is /// conditioned 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 /// 冰箱 33 /// </summary> 34 public class Refrigerator : TemperatureElectric 35 { 36 /// <summary> 37 /// 提升冷藏温度 38 /// </summary> 39 public override int Up() 40 { 41 if (temperature < 7) 42 temperature += 1; 43 return temperature; 44 } 45 46 /// <summary> 47 /// 降低冷藏温度 48 /// </summary> 49 public override int Down() 50 { 51 if (temperature > 3) 52 temperature -= 1; 53 return temperature; 54 } 55 }
Contrast V. several concepts
1, heavy and rewrite
Overloading and rewrite the same point that they are using the method of the same name.
Overloading and rewriting different points: on the use of the former in a single class, which uses two or more classes in the inheritance relations; on the intended use, the former is a simplified wording, after who is on extended functionality.
2, occlusion rewrites
Rewriting the same point that they are blocked by an extension of the base class method with the same name.
Rewriting occlusion difference is that, by A a = new B (); B inherits from A, B of the former call the method with the same name, which calls the same method on A; the former for active design, which is used passive modifications.
. 1 public class RealizeObject 2 { . 3 public void the Realize () . 4 { . 5 // Dog by virtual rewritable Animal . 6 Animal Animal = new new Dog (); . 7 animal.Swimming (); // dogs output plane . 8 . 9 / / Dog occluded by new Animal 10 Animal Animal = new Dog (); . 11 animal.Swimming (); // not output 12 } 13 }
3, virtual methods and abstract methods
The same point and virtual method abstract methods is that they can be rewritten.
Different virtual method abstract methods: the use of the former than the latter, the former in a non-sealed type, the latter can only be used in an abstract class; in use, former must implement portion, which is not for realizing portion; in a derived class, the former may not be rewritable rewritable, the latter must be rewritten.
4, the difference between the abstract class interface type (see C # interface type)