Definition
why?
public class Master { public void Feed(Dog dog) { dog.eat(); } public void Feed(Cat cat) { cat.eat(); } /* public void Feed(XXX xxx) //XXX Represents the parent class { xxx.eat(); } */ }
Suppose you have a master class. Feed different animals. Duplicate code is frequently modified when methods are invoked by different animal objects. Code is poorly scalable and maintainable.
So we can take an Animal class as a parameter and let different animals inherit it, which is polymorphism.
what?
Polymorphism is the ability of the same behavior to have multiple forms or forms of expression. For example, feeding the animals on it. Different animals feed different foods.
Many people do things with different results. For example, eagles fly. azaleas fly. magpies fly. They all fly. But their flying abilities are different. Some fly high while others fly low.
Polymorphism:
Polymorphisms can be static or dynamic. In static polymorphisms, method responses occur at compile time. In dynamic polymorphisms, method responses occur at run time.
Static polymorphism: At compile time, the connection mechanism between methods and objects is called early binding, also known as static binding. C#provides two techniques for achieving static polymorphism. Method overload, operator overload
Method overload: In the same class. Methods with the same name are allowed. Their number of parameters or parameter types are different.
Operator overload: Defines a function. Implements the desired function within the body of the function. When the operator is used, the compiler automatically calls the function. That is, operator overload is achieved through a function, which is essentially function overload.
Operator overload_Simon's blog-CSDN blog
Dynamic polymorphism: is achieved through abstract classes and virtual methods.
Two Elements Implementing Polymorphism (How)
1) Subclass overrides parent method
2) Use the type of parent class (subclass to parent: transition up)
class Animal { public virtual void call() { Console.WriteLine("Silent call"); } } class Dog : Animal { // The function of new is to hide the method of the same name of the parent class //public new void call() { //Console.WriteLine("Call: Wow-Wow-To"); //} public override void call() { Console.WriteLine("Call: Woo~Wong~Wong~"); } public void smell() { Console.WriteLine("It smells great!"); } } class Program { static void Main(string[] args) { Animal animal = new Dog(); //Upward Transition animal.call(); } } /*Output: Call: Wow~Wow~Wow~ */
Transition Up: Convert subclass objects to parent objects. Parent objects can be interfaces or abstract classes here.
The transformed parent object has all the methods of the parent class. If the method is override by a subclass, the override implementation is called when actually calling
Understand, through a multilevel inheritance chain, that "a parent type reference always calls the most recent version of the override function in the inheritance chain"
Special note: using the new keyword will destroy the polymorphism!
int a=(int)3.5; Dog dog = (Dog)animal;//Downward transition dog.call(); dog.smell();
Transition Down: Convert parent objects to child objects.
Transformed subclass objects, like inherited subclass objects in general. Has all methods of the subclass (including override methods) + all methods of the parent class (overridden methods, as overridden.)
case
Parent as parameter
class Animals { public virtual void Eat() { Console.WriteLine("eat"); } } class Dog : Animals { public override void Eat() { Console.WriteLine("Dogs eat bones"); } } class Cat:Animals { public override void Eat() { Console.WriteLine("Cats eat fish"); } } class Master { public void Feed(Animals a) //Parent class as parameter { a.Eat(); } } class Program { static void Main(string[] args) { //Anonymous object, call method once Animals a = new Animals(); Dog d = new Dog(); Cat c = new Cat(); a = b; //Upward Transition new Master().Feed(d); } }
Output: Dogs eat bones
Parent does return value
public Animals Feed(int i) //Parent class as parameter { Animals a = null; //Default Empty if (i==0) { a = new Dog(); } else if (i == 1) { a = new Cat(); } return a; } static void Main(string[] args) { //Anonymous object, call method once Animals a = new Animals(); Dog d = new Dog(); Cat c = new Cat(); a = d; //Upward Transition //new Master().Feed(d); a = new Master().Feed(1); a.Eat(); //Call Method }
Output: Cats eat fish
Parent makes parameter optimization
public void Feed(Animals a) //Parent class as parameter { //a.Eat(); /*Does this writing not directly determine whether a cat or a dog was passed in? So we have to make a judgement on the incoming a.*/ if (a is Dog) //Use is keyword { a.Eat(); } if (a is Cat) { a.Eat(); } } static void Main(string[] args) { //Anonymous object, call method once Animals a = new Animals(); Dog d = new Dog(); Cat c = new Cat(); a = d; //Upward Transition new Master().Feed(d); //a = new Master().Feed(1); //a.Eat();
Output: Dogs eat bones
Benefits of using polymorphism
-
The application does not have to write function calls for each derived class, it just needs to process the abstract base class. It greatly improves the reusability of the program. //Inheritance
-
Functions of derived classes can be invoked by base class methods or reference variables, which are called backward compatibility and improve scalability and maintainability. //The true role of polymorphism