Section 26 rewriting, polymorphism

Posted by ashmo on Mon, 20 Sep 2021 16:14:16 +0200

Class inheritance

  1. "Scale out" of class members (more and more members)
  2. "Vertical expansion" of class members (behavior change, version increase)
  3. Class member hiding (not commonly used)
  4. Overriding and hiding conditions: function members are visible (function members inherited from the parent class are visible to the child class), and the signature is consistent
    If the parent class member is decorated with protected, the child class can inherit, but cannot access the member
    Java rewrites naturally without modifiers

give an example:
(1) Method override:
Parent class members are required to be decorated with virtual and child class members with override

using System;

namespace OverrideExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Vehicle v = new Car();
            v.Run();
        }
    }

    //Implement the override of child class members to parent class members
    //Parent class members are required to be decorated with virtual and child class members with override

    //If neither parent nor child class members have modifiers,
    //It is called the hiding of child class members from parent class members
    class Vehicle
    {
        public virtual void Run()
        {
            Console.WriteLine("I'm running.");
        }
    }
  
    class Car : Vehicle
    {
        //Subclass overrides parent class members (vertical extension of members)
        public override void Run()
        {
            Console.WriteLine("Car is running.");
        }
    }
}

(2) Property member override

class Vehicle
    {
        private int _speed;
        //Speed attribute
        public virtual int Speed
        {
            get { return _speed; }
            set { _speed = value; }
        }
    }
 
class Car : Vehicle
    {
        private int _rpm;
        //Speed property override
        public override int Speed
        {
            get { return _rpm / 100; }
            set { _rpm = value / 100; }
        }
    }

(3) Hide
There are no virtual and override modifiers

Pay attention to the difference between rewriting and hiding!!!

polymorphism

  1. Based on rewriting mechanism (Virtual - > override)
    The essence of polymorphism: when the overridden member is called, the latest version related to the instance type on the inheritance chain can always be called

give an example:
(1)C#
Example 1:

using System;

namespace OverrideExample
{
    class Program
    {
        static void Main(string[] args)
        {
            //Variable of type Vehicle
            //When calling the Run method, go down level by level to find the latest version of Run
            Vehicle v = new RaseCar();
            v.Run();
        }
    }

    //Implement the override of child class members to parent class members
    //Parent class members are required to be decorated with virtual and child class members with override

    //If neither parent nor child class members have modifiers,
    //It is called the hiding of child class members from parent class members
    class Vehicle
    {
        public virtual void Run()
        {
            Console.WriteLine("I'm running.");
        }
    }
  
    class Car : Vehicle
    {
        //Subclass overrides parent class members (vertical extension of members)
        public override void Run()
        {
            Console.WriteLine("Car is running.");
        }
    }

    class RaseCar : Car
    {
        public override void Run()
        {
            Console.WriteLine("RaseCar is running.");
        }
    }
}

Example 2: the difference from the previous example is that the RaseCar.Run() method does not write the override modifier

using System;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

namespace OverrideExample
{
    class Program
    {
        static void Main(string[] args)
        {
            //Variable of type Vehicle
            //When calling the Run method, go down level by level to find the latest version of Run
            Vehicle v = new RaseCar();
            v.Run();
        }
    }

    //Implement the override of child class members to parent class members
    //Parent class members are required to be decorated with virtual and child class members with override

    //If neither parent nor child class members have modifiers,
    //It is called the hiding of child class members from parent class members
    class Vehicle
    {
        public virtual void Run()
        {
            Console.WriteLine("I'm running.");
        }
    }

    class Car : Vehicle
    {
        //Subclass overrides parent class members (vertical extension of members)
        public override void Run()
        {
            Console.WriteLine("Car is running.");
        }
    }

    class RaseCar : Car
    {
        public void Run()
        {
            Console.WriteLine("RaseCar is running.");
        }
    }
}

The variable v defined by Vehicle, from top to bottom, looks for the latest version of Run()

(2) There is no polymorphism in Python

# Python variables have no type, and the type of variables is consistent with the type of objects
# Therefore, there is no polymorphism in Python
class Vehicle:
    def run(self):
        print("I'm running!")

class Car(Vehicle):
    def run(self):
        print("Car is running!")

class RaseCar(Car):
    def run(self):
        print("Rase car is running!")

v = Vehicle()
v = RaseCar()
v.run()
  1. The specific behavior (version) of function members is determined by the object
  2. Review: variables and objects in C# language have types, so there will be "generation difference"

Supplement:
1) Access modifier
(1) public: any other code in the same assembly or other assemblies that reference the assembly can access the type or member.
(2) private: only code in the same class or structure can access the type or member.
(3) protected: a type or member that can only be accessed by code in the same class or structure or a derived class of this class.
(4) internal: any code in the same assembly can access the type or member, but code in other assemblies cannot.

2) Assembly
Two common assemblies are executable files (. exe files) and class library files (. dll files). In VS development environment, a solution can contain multiple projects, and each project is an assembly.

Topics: C#