C # study Polymorphism

Source: Internet
Author: User

Polymorphism means that the same operation acts on instances of different classes and different classes are interpreted to produce different execution results.

Polymorphism is implemented by using a derived class to override the virtual function method in the base class.

There are two types of polymorphism:

Polymorphism during compilation (static polymorphism)

Runtime polymorphism (Dynamic polymorphism)

Implementation of polymorphism:

Interface Polymorphism

Multiple classes can implement the same "interface", while a single class can implement one or more interfaces. The methods, attributes, and events that the interface description class needs to implement, and the parameter types that each member needs to receive and return. specific implementations of these members are implemented by the Implementation class.

Realize polymorphism through inheritance

Multiple classes can be inherited from a single base class. The class receives all methods, attributes, and events of the base class in the implementation of the base class. C # provide polymorphism through inheritance

Implement polymorphism through abstract classes

The abstract class itself cannot be instantiated and must be inherited. Its implementation is implemented by the derived class,

 

Class Program
{
Static void Main (string [] args)
{
Employee base1 = new Employee ();
Base1.CalculatePlay ();
Employee base2 = new SalariedEmployee (); // Polymorphism
Base2.CalculatePlay ();
EalariedEmployee Ebase = new SalariedEmployee (); // Polymorphism
Ebase. CalculatePlay ();
SalariedEmployee base3 = new SalariedEmployee ();
Base3.CalculatePlay ();
}
}
Public class Employee
{
Virtual public void CalculatePlay ()
{
Console. WriteLine ("Employee ");
}
}
Class SalariedEmployee: Employee
{
Public override void CalculatePlay ()
{
Console. WriteLine ("Salary ");
}
Public void ss ()
{
Console. WriteLine ("111 ");
}
}
Class EalariedEmployee: Employee
{
Public override void CalculatePlay ()
{
Console. WriteLine ("Ealaried ");
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.