Virtual (C # reference)
VirtualKeywords are used to modify methods, attributes, indexers, or event declarations. These objects can be rewritten in a derived class. For example, this method can be overwritten by any class that inherits it.
Copy code
Public Virtual double area () {return x * Y ;}
The Implementation of Virtual members can be changed by the override member in the derived class. UsageVirtualFor more information about keywords, see use override and new keywords for version control (C # Programming Guide) and learn when to use override and new keywords (C # programming guide ).
Remarks
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.
VirtualThe modifier cannot matchStatic,Abstract, privateOrOverrideModifier.
Except for the declaration and call syntax, the virtual attribute behavior is the same as that of the abstract method.
Example
In this example,DimensionsClass inclusionXAndYTwo coordinates andArea ()Virtual method. Different Shape classes, suchCircle,CylinderAndSphereInheritanceDimensionsClass, and calculate the surface area for each graph. Each derived class has its ownArea ()Rewrite implementation. According to the object associated with this method, by calling the correctArea ()Implementation,ProgramCalculate and display the correct area for each graph.
In the previous example, pay attention to the inherited classCircle,SphereAndCylinderAll use constructors that initialize the base class, for example:
Copy code
Public cylinder (Double R, double H): Base (R, h ){}
This is similar to the initialization list of c ++.
Copy code
// Cs_virtual_keyword.csusing system; Class testclass {public class dimensions {public const double Pi = math. pi; protected Double X, Y; Public dimensions () {} public dimensions (Double X, Double Y) {This. X = x; this. y = y;} Public Virtual double area () {return x * Y;} public class circle: Dimensions {public circle (Double R): Base (R, 0) {} public override double area () {return pI * x * X;} class sphere: Dimensions {public sphere (Double R): Base (R, 0) {} public override double area () {return 4 * pI * x * X;} class cylinder: Dimensions {public cylinder (Double R, double H): Base (r, h) {} public override double area () {return 2 * pI * x + 2 * pI * x * Y;} static void main () {double r = 3.0, H = 5.0; Dimensions c = new circle (r); dimensions S = new sphere (r); dimensions L = new cylinder (R, H ); // Display Results: console. writeline ("area of Circle = {0: F2}", C. area (); console. writeline ("area of Sphere = {0: F2}", S. area (); console. writeline ("area of cylinder = {0: F2}", L. area ());}}
Output
Area of Circle = 28.27 area of Sphere = 113.10 area of cylinder = 150.80
Override (C # reference)
You must useOverrideModifier.
In this exampleSquareRequiredAreaBecauseAreaIs abstracted fromShapesclassInherited.
Copy codeAbstract class shapesclass {Abstract Public int area ();} class square: shapesclass {int X, Y; // because shapesclass. area is abstract, failing to override // The Area Method wocould result in a compilation error. public override int area () {return x * Y ;}}
RelatedOverrideFor more information about keyword usage, see using override and new keywords for version control and understanding when to use override and new keywords.
Remarks
OverrideThe method provides a new implementation of Members inherited from the base class. PassOverrideThe method for declaring override is called the override base method. The override base method must beOverrideThe method has the same signature. For inheritance information, see inheritance.
You cannot override non-virtual or static methods. The override base method must beVirtual,AbstractOrOverride.
OverrideStatement cannot be changedVirtualMethod accessibility.OverrideMethod andVirtualThe method must have the same access level modifier.
Do not use ModifiersNew,Static,VirtualOrAbstractTo modifyOverrideMethod.
The override attribute declaration must specify the access modifier, type, and name exactly the same as the inherited attribute, and the overwritten attribute must beVirtual,AbstractOrOverride.