Polymorphism, as the name implies, is a variety of forms. It's like everyone makes a different choice about everything.
The first point of order for polymorphism to be expressed here is the application of the Richter scale substitution and polymorphism:
The Richter replacement is one of the important principles that should be adhered to in software design!! It is possible to inherit multiplexing with the Richter substitution rule, and the functionality of the software is not affected when the subclass replaces the parent class, the parent class is used normally, and the subclass can add new behavior on the basis of the parent class.
Let's write an example below:
// parent bird public class bird{ // Flight speed public double Speed{get ; set // fly void Fly () {}} // ostrich public class tuobird:bird{...
But we all know that ostriches are not flying, where the Fiy () method does nothing. This goes against the Richter scale replacement.
In order to solve this problem, there are two operators: IS and as
Is operator: used to check whether the object and the specified type are compatible
As operator: For type conversions between two objects
Parent class as parameter: The Richter substitution principle indicates that the subclass object can be substituted for the parent class object, then in the development program you can write a method with the parent class type as the formal parameter, and pass the subclass object in the actual call to achieve polymorphism.
Let's write an example below:
// transport parent class Traffic { // run public virtual void Run () {Console.WriteLine ( parent class Run ); } }
// car class Car:traffic { public override void Run () {Console.WriteLine ( " " ); } }
// bike class Bicycle:traffic { public override void Run () {Console.WriteLine ( " Xiaoming rode a very broken, broken-down, Phoenix-brand bike on March 1, 2016, happily driving in a muddy nim bumpy dirt road
"
); } }
// Employee Class Public class Employee { // go home with trafficpublic void gohome ( Traffic traffic) { traffic. Run (); } }
Abstract classes and abstract methods
How do you turn a normal class into an abstract class?
(1) Keywords: abstract
(2) Abstract classes cannot be instantiated
(3) The abstract method has no method body even {}
(4) If a class inherits an abstract class, then it must override all abstract methods in the abstract class.
(5) An abstract class cannot be static or sealed, such as a string
(6) Abstract methods exist in abstract class, abstract class can be used by ordinary class
(7) Abstract methods are only used to specify the method form of a subclass (parameter return value)
(8) Tell everyone the secret of the General people I do not say, there are shortcut keys ctrl+.+ enter
Let's talk about the difference between a virtual method and an abstract method.
| Virtual method |
Abstract methods |
| Modify with Virtual |
To modify with an abstract |
| To have a method body even a semicolon |
Method body is not allowed |
| You can override the quilt class |
You must override the quilt class |
| can be defined except for sealed classes |
can only be defined in abstract classes |
Have some knowledge about polymorphism--you deserve it.