Polymorphic learning Notes on object oriented programming mechanisms in C

  • 2020-12-18 01:54:23
  • OfStack

Polymorphism of C# :

The same operation will have different results when applied to different objects, that is, the same method will have different implementations when applied to different objects according to needs.

Polymorphism of C# includes: interface polymorphism, inheritance polymorphism.

Inherited polymorphism includes polymorphism realized by virtual method and polymorphism realized by abstract method

For example: base class animals have the method to eat, but different animals will eat different things, such as wolves eat meat, sheep eat grass, so "eat" this method will be in the derived class to achieve the following, run, through the pointer to the base class, to call the implementation of derived class method.

The following example implements polymorphism.

1. Interface polymorphism

Put the animal "eat" method into one interface (IAnimal), and then let the specific animal class (Wolf/Sheep) inherit the interface and implement the interface according to its needs.

Code implementation:


class Program {
        static void Main(string[] args) {
            new Wolf().Eat();
            new Sheep().Eat();
        }
    }     public class Wolf : IAnimal {
        // polymorphism
        public void Eat() {
            Console.WriteLine(" The Wolf eat meat! ");
        }
    }     public class Sheep : IAnimal {
        // polymorphism
        public void Eat() {
            Console.WriteLine(" Sheep eat grass! ");
        }
    }     // interface
    public interface IAnimal {
        void Eat();
    }

Interface polymorphism means that when different classes inherit the same interface, they have to re-implement the inherited interface according to their own needs, so that the same method signature in different classes will achieve different operations.

2. Inheritance polymorphism

2.1. Polymorphism achieved by virtual method (virtual,override)

You first implement the virtual method in the base class, and then override the virtual method with override in the derived class to suit your needs. If you do not want the method to be overridden, write the method as sealed.

The virtual method must be implemented in the base class.

Code implementation:


  class Program {
        static void Main(string[] args) {
            new Wolf().Eat();
            new Sheep().Eat();
            new Goat().Eat();
        }
    }     public class Wolf : Animal {
        // polymorphism
        public override void Eat() {
            base.Eat();
            Console.WriteLine(" The Wolf eat meat! ");
        }
    }     public class Sheep : Animal {
        // polymorphism
        public override void Eat() {
            base.Eat();
            Console.WriteLine(" Sheep eat grass! ");
        }
    }     public class Goat : Sheep {
        // The polymorphic implementation is terminated , this Eat Method cannot be override Because with sealed the
        public sealed override void Eat() {
            //base.Eat();
            Console.WriteLine(" Goats eat grass! ");
        }
    }     // The base class implements virtual methods
    public class Animal {
        public virtual void Eat() { }
    }
 

2.2. Polymorphism achieved by abstract methods (abstract,override)

Abstract methods must be defined in an abstract class. An abstract class cannot be created.

Abstract methods in base classes can only be declared and do not need to be implemented, so there are no base methods when overriding abstract methods in derived classes.

The code implementation is as follows:


class Program {
        static void Main(string[] args) {
            new Wolf().Eat();
            new Sheep().Eat();
            new Goat().Eat();
        }
    }     public class Wolf : Animal {
        // polymorphism
        public override void Eat() {
            Console.WriteLine(" The Wolf eat meat! ");
        }
    }     public class Sheep : Animal {
        // polymorphism
        public override void Eat() {
            Console.WriteLine(" Sheep eat grass! ");
        }
    }     public class Goat : Sheep {
        // The polymorphic implementation is terminated , this Eat Method cannot be override Because with sealed the
        public sealed override void Eat() {
            Console.WriteLine(" Goats eat grass! ");
        }
    }     // The base class simply declares the methods
    public abstract class Animal {
        public abstract void Eat();
    }

Conclusion:

1. Virtual methods can be overridden with base (base.Eat ()), while abstract methods cannot be overridden with base. The reason is that virtual methods must be implemented in the base class, while abstract methods are only declared in the base class and do not need to be implemented.

2. Derived classes may not override the implementation of virtual methods, but derived classes must override the implementation of abstract methods, for the same reason.

3. Non-abstract classes that contain virtual methods can be created instances (objects), but abstract classes that contain abstract methods cannot be created instances.

A derived class that inherits an interface must implement the interface's methods, because the interface is also responsible for declaring methods, not implementing them.

5. The polymorphism of the interface does not need to use the override rewriting method.


Related articles: