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 not the virtual member cannot be overwritten 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 Method And Abstract attributes It cannot be called directly. It can only be called after it is implemented through a derived class. Virtual Method And Virtual attributes It 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 private Or 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 attributes Virtual The modifier is incorrect.
② Use Override Modifier attribute declaration. You can override the virtual inheritance attribute in a derived class.
Virtual Method
If the declaration of an instance method contains Virtual Modifier, which is called a virtual method. If no Virtual 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 void F () {console. writeline ("a.f ");}
Public Virtual void G () {console. writeline ("A. G ");}
}
Class B:
{
New Public Void F () {console. writeline ("B. F ");}
Public Override Void G () {console. writeline ("B .g ");}
}
Class Test
{
Static void Main (){
B = New B ();
A AB = B; // here, Class A is a Class B object.
A A = New A ();
A. F (); // Print a.f
B. F (); // Print B. f
AB. F (); // print a.f
A. G (); // Print a. 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!
Reprinted from: Http://blog.sina.com.cn/s/blog_48a45b950100ev9a.html