C # Keyword -- virtual Virtual Keywords are used to modify methods, attributes, indexers, or event declarations, and enable them to be overwritten in a derived class. The Implementation of Virtual members can be changed by the override member in the derived class, but non-Virtual members cannot be rewritten by the derived class. This is different from Java. In Java, as long as the method with the same signature as the parent class is defined in the derived class, the method of the parent class is overwritten. In C # language, the virtual keyword must be used for modification. Otherwise, rewriting in the derived class will cause the compiler to report an error.
Virtual methods or virtual attributes are not equivalent to abstract methods or abstract attributes..Abstract MethodAndAbstract attributesIt cannot be called directly. It can only be called after it is implemented through a derived class.Virtual MethodAndVirtual attributesIt indicates that the virtual method and virtual attribute in the parent class may be overwritten. 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,Non-virtual methods cannot be overwritten (unless the new keyword is used).Virtual
The modifier cannot matchStatic,Abstract and privateOr
Override
Modifier.Except for the declaration and call syntax, the virtual attribute behavior is the same as that of the abstract method. Note:
①Use static attributesVirtualThe modifier is incorrect.
②UseOverride
Modifier attribute declaration. You can override the virtual inheritance attribute in a derived class.
Virtual Method
If the declaration of an instance method containsVirtual
Modifier, which is called a virtual method. If noVirtual
Modifier, it is called a non-virtual method. The implementation of non-virtual methods will not change: whether it is to call this method on the instance that declares its class or call it on the instance of the derived class, the implementation is the same. In contrast, the implementation of a virtual method can be replaced by a derived class. The process of replacing the implementation of the inherited virtual method is called rewriting this method.
Class
A
{
Public voidF (){
Console. writeline ("a.f ");}
Public Virtual void
G () {console. writeline ("A. G ");}
}
Class B:
A
{
New
Public VoidF (){
Console. writeline ("B. F ");}
Public OverrideVoidG (){
Console. writeline ("B .g ");}
}
Class Test
{
Static voidMain ()
{
B =New
B ();
A AB = B; // here, Class A is a Class B object.
A A =NewA ();
A. F (); // print a.f
B. F (); // print B. f
AB. F (); // print a.f
A. G (); // print B .g
B. G (); // print B .g
AB. G (); // print B .g
}
}
Note: The results returned by A. F () and AB. G () show that there is a difference between using the new and override keywords to rewrite the method.
- The base class object calls the method corresponding to the base class, whether it is a virtual method or not.
- There are two types of derived class objects:
① Reference declared as a derived class variable: Call the method of the derived class
② Reference Declaration as a base variable:
■ If override is used for rewriting, the overwritten method in the derived class is called.
■ If new is used for rewriting, the base class method is called.
Note: The above principles applyVirtual attributesThe same applies! Original article: http://blog.sina.com.cn/s/blog_48a45b950100ev9a.html