C # Tutorial C # polymorphism

Source: Internet
Author: User

C # polymorphism

Polymorphism implies a multiplicity of forms. In object-oriented programming paradigm, polymorphism often manifests as "one interface, multiple functions".

Polymorphism can be static or dynamic. In static polymorphism, the response of a function occurs at compile time. In dynamic polymorphism, the response of a function occurs at run time.

Static polymorphism

At compile time, the connection mechanism of functions and objects is called early binding, also known as static binding. C # provides two techniques for static polymorphism. The following were:

function overloading

Operator overloading

Operator overloading will be discussed in the next section, and we'll talk about function overloading next.

function overloading

You can have multiple definitions of the same function name within the same scope. Functions must be defined differently from each other, either as parameter types in the parameter list, or with different number of arguments. You cannot overload a function declaration that has only a different return type.

The following example shows several of the same function print (), which are used to print different data types:

Using System;namespace polymorphismapplication{   class printdata   {      void print (int i)      {         Console.WriteLine ("Printing int: {0}", i);      }      void print (double f)      {         Console.WriteLine ("Printing float: {0}", f);      }      void print (string s)      {         Console.WriteLine ("Printing string: {0}", s);      }      static void Main (string[] args)      {         Printdata p = new Printdata ();         Call Print to plot the integer         p.print (5);         Call print for printing floating         -point p.print (500.263);         Called Print to print         the string p.print ("Hello C + +");         Console.readkey ();}}}   

When the above code is compiled and executed, it produces the following results:

Printing int:5printing float:500.263printing String:hello C + +

Dynamic polymorphism

C # allows you to use keyword abstraction to create an abstract class that provides an implementation of a partial class of interfaces. When a derived class inherits from the abstract class, the implementation is complete. Abstract classes contain abstract methods, and abstract methods can be implemented by derived classes. Derived classes have more specialized functionality.

Note that here are some rules about abstract classes:

You cannot create an instance of an abstract class.

You cannot declare an abstract method outside an abstract class.

You can declare a class as a sealed class by placing the keyword sealed in front of the class definition. When a class is declared as sealed, it cannot be inherited. Abstract classes cannot be declared as sealed.

The following program demonstrates an abstract class:

Using System;namespace polymorphismapplication{   abstract class Shape   {public      abstract int area ();   }   Class Rectangle:  Shape   {      private int length;      private int width;      Public Rectangle (int a=0, int b=0)      {         length = A;         width = b;      }      public override int Area ()      {          Console.WriteLine ("Size of the Rectangle class:");         Return (width * length);       }   }   Class Rectangletester   {      static void Main (string[] args)      {         Rectangle r = new Rectangle (7);         Double A = R.area ();         Console.WriteLine ("Area: {0}", a);         Console.readkey ();}}}   

When the above code is compiled and executed, it produces the following results:

Area of the Rectangle class: area: 70

Virtual methods can be used when there is a function defined in the class that needs to be implemented in the inheriting class. Virtual methods are declared using the keyword virtual. Virtual methods can have different implementations in different inheritance classes. A call to a virtual method occurs at run time.

Dynamic polymorphism is achieved through abstract classes and virtual methods.

The following program demonstrates this:

Using System;namespace polymorphismapplication{class Shape {protected int width, height;         Public Shape (int a=0, int b=0) {width = A;      height = b;         } public virtual int area () {Console.WriteLine ("The size of the parent class:");      return 0;  }} class Rectangle:shape {public Rectangle (int a=0, int. b=0): Base (A, B) {} public override         int area () {Console.WriteLine ("Size of the Rectangle class:");       Return (width * height); }} class Triangle:shape {public Triangle (int a = 0, int. b = 0): Base (A, B) {} public O         verride int Area () {Console.WriteLine ("Size of the Triangle class:");       Return (width * height/2);         }} class Caller {public void Callarea (Shape sh) {int A;         A = Sh.area ();      Console.WriteLine ("Area: {0}", a); }} class Tester {static void Main (string[] args) {Caller c =New Caller ();         Rectangle r = new Rectangle (10, 7);         Triangle t = new Triangle (10, 5);         C.callarea (R);         C.callarea (t);      Console.readkey (); }   }}

When the above code is compiled and executed, it produces the following results:

Size of the Rectangle Category: Area: 70Triangle Area: Area: 25

The above is the "C # tutorial" C # polymorphism content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!


  • 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.