This article describes the following:
What is overwrite, what is overload
The difference between overriding and overloading
Application of covering and overloading in polymorphic properties
1. Introduction
Overwrite (override) with overloading (overload), is the achievement. NET object-oriented polymorphism characteristics, one of the basic technologies, two seemingly similar but in fact not the concept, often brings us a lot of misunderstanding, so it is necessary to discuss the difference between the topic, and more importantly, pay attention to its application in polymorphism.
In the series, we have a bit of the topic of the discussion, this article in the form of a topic to do the in-depth discussion, the relevant content please do the previous article for reference.
2. Understanding Overwrite and overload
Start with an example to understand what is overwrite and what is overload?
Abstract class Base
{
//Definition virtual method
Public virtual void MyFunc ()
{
the
//Parameter list is different, virtual is not sufficient to differentiate between
public virtual void MyFunc (String str)
{
}
//Parameter list is different, the return value is different
public bool MyFunc (string str, int id)
{
Console.WriteLine ("AAA");
return true;
}
//argument list differs in number of occurrences, or in different parameter types of the same position
public bool MyFunc (int ID, string str)
{
Console.WriteLine ("BBB");
return false;
}
//generic overload, allow the same argument list
public bool Myfunc<t> (string str, int id)
{
return true;
}
///define abstract method
Public abstract void Func ();
Class Derived:base
{
//Blocking parent class member
public new void MyFunc ()
{
}
//Overwrite base class member
public override void MyFunc (String str)
{
Accesses the parent class member,
Base, in a subclass. MyFunc (str);
}
//Overwrite base class abstract method
public override void Func ()
{
//implementation Overwrite method
}