C # Learning Diary---Operator overloading, method overloading, abstract class, virtual method of polymorphism

Source: Internet
Author: User
The definition of polymorphism in C # is that the same operation works on instances of different classes, different classes are interpreted differently, and results are different. In other words, an interface, multiple functions.

C # supports 2 forms of polymorphism: compile-time polymorphism, run-time polymorphism

Compile-time polymorphism:

Compile-time polymorphism is implemented by overloading

Method 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. Write an example

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Test  {      class Exchange  //define an Exchange class      {//Method implementation interchange two-parameter data public          void swap (int a, int b)          {              int temp;              temp = A;              A = b;              b = temp;              Console.WriteLine ("{0},{1}", A, b);          }          public void Swap (string A, string b)          {              string temp;              temp = A;              A = b;              b = temp;              Console.WriteLine ("{0},{1}", A, b);          }        }      Class program      {          static void Main (string[] args)          {              Exchange exch = new Exchange ();              Exch.swap (ten);  Call Swap (int A,int b) method              Exch.swap ("large", "small");  Call Swap (string a,string B) method                    }}}  

Results:

Operator overloading

Using System;  Using System.Collections.Generic;  Using System.Linq;    Using System.Text;          Namespace Test {class Student//define student class {private int Chinese;          private int Math;              public void value (int a, int b)//Define an assignment method, and later learn the construction method is not so troublesome {Chinese = A;          Math = b; } public static student operator + (student A, student B)//operator overloading, implementing the Add function {student Stu = NE              W student (); Stu.              Chinese = A.chinese + B.chinese; Stu.              Math = A.math + B.Math;          return Stu;          } public int Getchinese ()//Get Chinese method {return Chinese;          } public int Getmath ()//Get Math method {return math; }} class Program {static void Main (string[] args) {Student A = new Studen              T ();              Student B = new student ();      A.value (70,80);        B.value (40, 50);  Student Stu = a + b;                        70+40, 80+50 Console.WriteLine ("A+b Chinese = {0}\na+b Math = {1}", Stu.getchinese (), Stu.getmath ()); }      }  }

Results:

Run-time polymorphism:

Runtime polymorphism refers to what happens when the system is running, depending on the actual situation, and the polymorphic state of the runtime in C # is implemented by an abstract class or virtual method.

Abstract classes and abstract methods

C # allows you to create abstract classes or abstract methods using the keyword abstract, which is accomplished when a derived class inherits from the abstract class. Abstract classes contain abstract methods, and abstract methods can be implemented by derived classes. Derived classes have more specialized functionality. Abstract classes cannot be instantiated,

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Test  {//Create abstract class and abstract method abstractly classes     score      {public         abstract int Add ();      }      Create subclass class     Student:score     {         private int chinese = +;         private int Math = n;         public override int Add ()  //Keyword Override instance method         {            int sum=chinese+math;             return sum;         }              }      Class program      {          static void Main (string[] args)          {              student stu = new student ();                              Console.WriteLine (Stu. ADD ());  Result (*)}}}  

Virtual method

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.

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Test  {     class score      {         protected int chinese =;         protected int Math = n;         public virtual int ADD ()  //Define a virtual method         {             int sum = chinese + Math;             return sum;         }      }     Define subclass, implement method     class Student:score     {public         override int Add ()  //Keyword Override instance method, implement subtract operation         {             int sub = Math-chinese;             return sub;         }              }      Class program      {          static void Main (string[] args)          {              student stu = new student ();                              Console.WriteLine (Stu. ADD ());  Result (Ten}}}  

We can see that the method that the runtime actually calls is not a virtual method, but a method after the override instance

The above is the C # Learning diary---Polymorphism of operator overloading, method overloading, abstract class, virtual method of the 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.