C # Polymorphism

Source: Internet
Author: User

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 ;}}}


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.