C # differences between polymorphism, overloading, and rewriting

Source: Internet
Author: User

Override refers to the method to override the base class. The methods in the base class must have the virtual modifier, and override must be specified in the subclass method.
Format:
Base class:
Public virtual void myMethod ()
{
}
Subclass:
Public override void myMethod ()
{
}
After rewriting, The myMethod () method is accessed by the base class object and the subclass object. The result is all the methods that are accessed and redefined in the subclass. The base class method is overwritten.

Heavy Load
It is used to select an optimal function member to implement the call when the parameter list and a set of candidate function members are given.
Public void test (int x, int y ){}

Public void test (int x, ref int y ){}

Public void test (int x, int y, string ){}
Overload features:

I. The method name must be the same

II. The parameter list must be different and irrelevant to the order of the parameter list

III. the return value types can be different.
======================================
But if there is a generic type, you should pay attention to it!

Polymorphism
C # polymorphism is mainly reflected in class inheritance:
When a subclass inherits the parent class, the same name may occur but the method definition is different,
Therefore, the original method will be overwritten in the subclass to meet its own requirements.

1/* 2 Function: override 3 */4 using System; 5 namespace TestOverride 6 {7 class Employee 8 {9 // parent class virtual method 10 public virtual void CalculatePay () 11 {12 Console. writeLine ("Employee"); 13} 14} 15 16 // subclass override CalculatePay () method 17 class SalariedEmploy: Employee18 {19 public override void CalculatePay () 20 {21 Console. writeLine ("Salary"); 22} 23} 24 25 class AppPay26 {27 public static void Main (String [] args) 28 {29 // create a parent class instance 30 Employee employee1 = new Employee (); 31 employee1.CalculatePay (); // Employee32 33 // assign a subclass value to the parent class 34. Employee employee2 = new SalariedEmploy (); 35. employee2.CalculatePay (); // Salary36 37 // subclass creates an instance 38 SalariedEmploy employee3 = new SalariedEmploy (); 39 employee3.CalculatePay (); // Salary40 41} 42} 43} 44/* 45 Out: 46 Employe47 Salary48 Salary49 */View Code

 

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.