C# polymorphic knowledge points

Polymorphism
1. Definition:
What: Polymorphism is the ability of the same behavior to have multiple different manifestations or forms Vernacular
expression: Let an object show multiple states (types)
Refined expression: Realized through inheritance Different objects call the same method and show different behaviors, which is called polymorphism (the teacher said that it was used during the interview, but I have not tried it yet)
Why: Solve the problem of frequent modification of repeated code

2. Application of polymorphism
Three methods to realize polymorphism
a. virtual method
b. abstract class
c. interface

3. Two elements to realize polymorphism
1) Subclass rewrites parent class method
2) Transformation

4. Benefits:
1) Substitutability (subst ut ablity). Polymorphism enables substitutability for existing code.
2) Scalability (extensibility). Polymorphism is extensible to code. Adding new subclasses does not affect the operation and operation of polymorphism, inheritance, and other features of existing classes.
3) Interface-ablity. Polymorphism is achieved by superclasses providing a common interface to subclasses through method signatures, which are then completed or overwritten by subclasses.
4) Flexibility. It embodies flexible and diverse operations in the application and improves the efficiency of use.
5) Simplicity (simplicity). Polymorphism simplifies the code writing and modification process of application software, especially when dealing with the calculation and operation of a large number of objects, this feature is particularly prominent and important.

5. Upward transformation and downward transformation
Upward transformation: the parent class reference points to the subclass object
For example: Animal anima = new Dog();
Downward transformation: the subclass reference points to the parent class object
For example: Dog dog = (Dog) Animal; No Security
6. Case

class Animal
  {
    
    
   public virtual void Eat()//虚方法
     {
    
    
       Console.WriteLine("动物吃饭");
     }
  }
class Dog : Animal
  {
    
    
    public int age = 2;
    public override void Eat()//子类重写父类
     {
    
    
      Console.WriteLine("狗吃骨头");
     }
  }
class Cat : Animal
  {
    
    
   public string sex = "公";
   public override void Eat()//子类重写父类
    {
    
    
      Console.WriteLine("猫吃鱼");
    }
  }
//is用于判断一个类型是否是一种类型   son is Person :真或假
//as用于引用类型转换   成功则为非空,失败则为空  son as Person:null或对象
class Master
 {
    
    
  void Feed(Animal a)//父类做参数
   {
    
    
    if (a is Dog)//判断a是狗
     {
    
    
      // Dog d =(Dog) a;//向下转型
      Dog d = a as Dog;//向下转型
      d.age = 2;
      Console.WriteLine("狗的年龄:" + d.age);
     }
    else if (a is Cat)//判断a是猫
     {
    
    
       Cat c = (Cat)a;//向下转型
       c.sex = "母";
       Console.WriteLine("猫的性别:" + c.sex);
     }
      a.Eat();
 }
static void Main(string[] args)
  {
    
    
   Master m = new Master();
   Animal dog = new Dog();//向下转型    
   m.Feed(dog);
   Animal cat = new Cat();//向下转型
   m.Feed(cat);   
  }

//It looks a bit confusing, as long as you know what is an upward transition and what is a downward transition
//There are two elements that polymorphism needs to have:
//a. The subclass overrides the parent class method
//b. transformation

Guess you like

Origin blog.csdn.net/weixin_44706943/article/details/125959638
Recommended