New
The New Keyword can explicitly hide members inherited from the base class. Hiding inherited members means that the derived version of the member replaces the base class version.That is to say, the method used to use the derived class is calledNewKeywordsNew definitionInstead of the base class method.
Do not use
The new modifier is allowed to hide members, but a warning is generated. Explicitly hiding a member with new removes this warning and records the fact that the member is replaced with a derived version.
Virtual
Virtual Keywords allow re-writing these objects in a derived class. By default, the method is non-virtual and cannot be rewritten, Virtual Keywords cannot match Static , Abstract , Private , Override .
Virtual The keyword is sum. Override Closely related, if you want to implement Virtual Method is required Override Or New Keyword ( New And Override ).
Abstract
Use in class declarationAbstractModifier to indicate that a class can only be the base class of other classes. Members marked as abstract or included in an abstract class must be implemented through classes derived from the abstract class.Abstract classes cannot be instantiated.
Abstract method declaration does not provide actual implementation, so there is no method body; method declaration ends with a semicolon and there is no braces after the signature({}).
Sealed
The sealing method will override the methods in the base class, but it cannot be further rewritten in any derived class.The sealed class cannot be inherited.
When applied to methods or attributes,SealedThe modifier must always beOverride.
Override
OverrideThe keyword is mainly to provide a new implementation of the derived class to the base class method. The overwritten base class method must beOverrideThe method has the same signature
The following example uses the example words as a simple example. Examples are more complex than examples!
The main idea of the example is as follows:
Category
B:
Abstract C: B
Sealed D: c
The process for the methods included in the class is as follows:
Method
New
Virtual
Abstract
Override
Sealed override
Class Program
{
/// <Summary>
/// Define a basic "mybaseclass"
/// </Summary>
Public Class Mybaseclass
{
/// <Summary>
/// Define a method
/// </Summary>
Public Void DOA ()
{
Console. writeline ( " Mybaseclass. Base DOA () " );
}
}
/// <Summary>
///Define the memory class myinheritclass class for mybaseclass
/// </Summary>
Public ClassMyinheritclass: mybaseclass
{
/// <Summary>
/// This indicates the DOA method in the baseclass of the hidden class and the deduplication method.
/// </Summary>
New Public Void DOA ()
{
Console. writeline ( " Mybaseclass. inherit base DOA () " );
// The DOA method of mybaseclass Based on operation class
Base . DOA ();
}
/// <Summary>
/// Define a Processing Method
/// </Summary>
Public Virtual Void DOB ()
{
Console. writeline ( " Mybaseclass. inherit base DOB () " );
}
}
/// <Summary>
/// Definition abstraction class myabstractclass class inherit class myinheritclass
/// </Summary>
/// Abstract
Public Abstract Class Myabstractclass: myinheritclass
{
/// <Summary>
/// Define the methods of myinheritclass and redefine the abstract methods.
/// </Summary>
New Public Abstract Void DOA ();
/// <Summary>
/// Repeat the base class myinheritclass method, which is a new method
/// </Summary>
Public Override Void DOB ()
{
Console. writeline ( " Mybaseclass. Abstract override DOB () " );
//At this time, the methods in myinheritclass will be used.
Base. DOA ();
}
}
/// <Summary>
/// It is agreed that the class mysealedclass should be sealed and the class myabstractclass should be sealedclass.
/// </Summary>
Public Sealed Class Mysealedclass: myabstractclass
{
/// <Summary>
/// Abstract method of repeat base class myabstractclass
/// </Summary>
Public Override Void DOA ()
{
Console. writeline ( " Mybaseclass. Sealed DOA () " );
}
/// <Summary>
/// Method for sealing duplicate myabstractclass
/// </Summary>
Public Sealed Override Void DOB ()
{
Console. writeline ( " Mybaseclass. Sealed override DOB () " );
}
Public Void Callbase ()
{
// At this time, instead of using the basic class mybaseclass (abstract class) method, the method of using the myabstractclass class is used.
Base . DOB ();
// Click here. Because the abstract method cannot be used as the original method
// Base. DOA ();
}
}
static void main ( string [] ARGs)
{< br> /// practically base classes and using base classes
mybaseclass baseclasee = New mybaseclass ();
baseclasee. DOA ();
/// examples, and inherit the classification method
myinheritclass inheritcalss = New myinheritclass ();
inheritcalss. DOA ();
inheritcalss. DOB ();
/// practically sealed, use the
mysealedclass sealedclass = New mysealedclass ();
sealedclass. DOA ();
sealedclass. DOB ();
sealedclass. callbase ();
console. readkey ();
}
}
/*
* Returns the result.
* Mybaseclass. Base DOA ()
*
* Mybaseclass. inherit base DOA ()
* Mybaseclass. Base DOA ()
* Mybaseclass. inherit base DOB ()
*
* Mybaseclass. Sealed DOA ()
* Mybaseclass. Sealed override DOB ()
*
* Mybaseclass. Abstract override DOB ()
* Mybaseclass. inherit base DOA ()
* Mybaseclass. Base DOA ()
*/