Abstract, virtual, override, new description

Source: Internet
Author: User
I have always been vague about the similar promise. Today I am lucky to see an article Article , I think it's quite good. I just thought it was over!

Abstract
Abstract modifiers can be used with classes, methods, attributes, indexers, and events.

Use the abstract modifier in the class declaration to indicate that the class can only be the base class of other classes.

Abstract classes have the following features:
· The abstract class cannot be instantiated.
· Abstract classes can contain abstract methods and abstract accessors.
· The abstract class cannot be modified with the sealed modifier, which means the class cannot be inherited.
· A non-abstract class derived from an abstract class must include all the inherited abstract methods and the actual implementation of the abstract accessors.
· Use the abstract modifier in a method or attribute declaration to indicate that this method or attribute does not contain an implementation.

Abstract methods have the following features:
· The abstract method is an implicit virtual method.
· Only abstract methods can be declared in abstract classes.
· Because the abstract method declaration does not provide real implementation, there is no method body. The method declaration ends with a semicolon and there is no braces ({}) after the signature ({}). Example: public abstract void mymethod ();
· The implementation is provided by the overriding method, which is a non-abstract class member.
· It is incorrect to use the static or virtual modifier in the abstract method declaration.

Except for the differences in the declaration and call syntax, abstract attributes behave the same way as abstract methods.
· It is incorrect to use the abstract Modifier on static attributes.
· In a derived class, you can override abstract inherited attributes by using the attribute declaration using the override modifier.

Virtual
The virtual keyword is used to modify the declaration of a method or attribute. In this case, a method or attribute is called a virtual member. The Implementation of Virtual members can be changed by the override member in the derived class.

When a virtual method is called, The system checks the runtime type of the object for the override member. This override member in most Derived classes will be called. If no derived class is used to override this member, it may be the original member.

By default, the method is non-virtual. You cannot override non-virtual methods.

Virtual modifiers cannot be used with the following modifiers:
Static abstract override

Except for the declaration and call syntax, the virtual attribute behavior is the same as that of the abstract method.
· It is incorrect to use the virtual Modifier on static attributes.
· You can override a virtual inheritance attribute in a derived class by including the attribute declaration using the override modifier.

Override (overwrite the above two keyword modifier methods)
Use the override modifier to modify methods, attributes, indexers, or events. The override method provides a new implementation of Members inherited from the base class. The method overridden by the rewrite statement is called the override base method. The override base method must have the same signature as the override method.

You cannot override non-virtual or static methods. The override base method must be virtual, abstract, or rewritten.

Override declaration cannot change the accessibility of virtual methods. The override method and virtual method must have the same access level modifier.

You cannot use the following modifier to modify the rewrite method:
New Static Virtual Abstract

The override attribute declaration must specify the access modifier, type, and name exactly the same as the inherited attribute, and the override attribute must be virtual, abstract, or overwritten.

Using System;
 
Namespace Vitualmethod
{
Class Beiseclass
{
Public   Void Realmethod ()
{
Console. writeline ( @" The "" realmethod "" method of "" beiseclass "" class! " );
}
 
Public   Virtual   Void Viutualmethod ()
{
Console. writeline ( @" The "" viutualmethod "" method of "" beiseclass "" class! " );
}
}
 
Class Newandoverride: beiseclass
{
/**//**//**/ ///   <Summary>  
/// "New" is used to hide the inherited members of the base class members. It is another method of rewriting;
/// It is incorrect to use both new and override on the same member;
/// "Override" only hides virtual methods.
///   </Summary>  
New   Public   Void Realmethod ()
{
Console. writeline ( @" The "" realmethod "" method of "" newandoverride "" class! " );
}
 
Public   Override   Void Viutualmethod ()
{
Console. writeline ( @" The "" viutualmethod "" method of "" newandoverride "" class! " );
}
}
 
Class Tester
{
/**//**//**/ ///   <Summary>  
/// Running result:
/// The "realmethod" method of "beiseclass" class!
/// The "realmethod" method of "newandoverride" class!
/// The "viutualmethod" method of "beiseclass" class!
/// The "viutualmethod" method of "newandoverride" class!
///
/// The "realmethod" method of "beiseclass" class!
/// The "viutualmethod" method of "newandoverride" class!
///   </Summary>  
Static   Void Main ()
{
Beiseclass =   New Beiseclass ();
Newandoverride =   New Newandoverride ();

Beiseclass. realmethod ();
Newandoverride. realmethod ();

 
Beiseclass. viutualmethod ();
Newandoverride. viutualmethod ();
 
Console. writeline ();
 
//  
// Note: The Hidden method still exists, and the overwritten method does not exist;
//  
Beiseclass = Newandoverride;

Beiseclass. realmethod ();
Beiseclass. viutualmethod ();
 
}
}
}

New
Use the new modifier to explicitly hide the members inherited from the base class. To hide an inherited Member, declare the member in the derived class with the same name and modify it with the new modifier.

The class member declaration can use the same name or signature as an inherited member to declare a member. In this case, the derived class member hides the base class member. Hiding an inherited member is not an error.
But this does cause the compiler to issue a warning. To cancel this warning, the declaration of a derived class member can contain a new modifier, indicating that the derived member intentionally hides the base member.

Using System;
 
Namespace Thenewkeyword
{
 
Class Newtestclassbase
{
Public   Void Printnewkeyword ()
{
Console. writeline ( @" This is base class! " );
}
}
 
Class Newtestclass: newtestclassbase
{
/**//**//**/ ///   <Summary>  
/// Write the following statement:
/// Override public void printnewkeyword ()
///
/// Compilation error:
/// "Thenewkeyword. newtestclass. printnewkeyword ()":
/// The inherited member "thenewkeyword. newtestclassbase. printnewkeyword ()" cannot be rewritten ()",
/// Because it is not marked as virtual, abstract, or override.
///   </Summary>  
New   Public   Void Printnewkeyword ()
{
Console. writeline ( @" This is "" new "" keyword! " );
}
}
 

/**//**//**/ ///   <Summary>  
/// Thenewkeyword test the keyword "new.
///   </Summary>  
Class Thenewkeyword
{
/**//**//**/ ///   <Summary>  
/// Running result:
/// This is base class!
/// This is "new" keyword!
///   </Summary
Static void main ()
{
//  
// Todo: addCodeTo start the applicationProgram 
//  
Newtestclassbase =   New Newtestclassbase ();
Newtestclass =   New Newtestclass ();
 
Newtestclassbase. printnewkeyword ();
Newtestclass. printnewkeyword ();
}
}
}






Auto: http://www.cnblogs.com/nbwzy/articles/440370.html

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.