1. Meaning of Polymorphism
The same operation acts on different objects and can have different interpretations to generate different execution results.
2. polymorphism type
(1) polymorphism during compilation
During compilation, polymorphism is implemented through overloading. For non-Virtual members, the system determines the operation to be implemented based on the transmitted parameters, returned types, and other information during compilation.
(2) Runtime polymorphism
The running polymorphism refers to the operation that can be performed only when the system is running.
C # The Running polymorphism is implemented by overwriting virtual members.
3. Heavy Load and overwriting
(1) heavy load:
The method name must be the same;
The parameter list must be different;
The return value types can be different.
(2) Overwriting
Subclass defines different implementations of a method to meet its own needs. Override keywords are used to implement overwriting. Only virtual and abstract methods can be overwritten.
Features:
The method name is the same;
The parameter list is the same;
The return value type is the same.
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication13{ class Program {
// Call static void Main (string [] args) {Square tt = new Square (); Console. writeLine (tt. getsides (); Triangle dd = new Triangle (); Console. writeLine (dd. getsides ());}}////// Define a Sharp chart class ///Abstract public class Sharp {public abstract int Getsides ();}/////// Derived class: Square and Triangle ///Public class Square: Sharp {public override int Getsides () {return 1 ;}} public class Triangle: Sharp {public override int Getsides () {return 2 ;}}}