Abstract
- Abstract methods are implicit virtual methods
- In an abstract method declaration, you cannot use the static or virtual modifier
Override
- The override keyword provides a new implementation of a derived class for a base class method, overriding a base class method that must have the same signature (function name, return value, parameter list) as the method of the base class.
- The override keyword can not override the base class's non-virtual-decorated methods and static methods of static adornments. The override method of a derived class overrides the base class method that must be virtual, abstract, or override.
- The override method of a derived class and the virtual method of the base class must have the same access rights
- The override method cannot be modified with the modifier new, static, virtual, or abstract.
New
- a virtual method that overrides the parent class as a modifier, which runs a function of the subclass when called with the type of the subclass, and if the type is a base class, the hidden base class function is called . When a function in a subclass uses override, the child class function is called when the subclass's type is called, and it runs the function of the subclass, which is the base class.
classCar { Public Virtual voidDescribeCar () {System.Console.WriteLine ("Four wheels and an engine."); } } classConvertiblecar:car { Public New voidDescribeCar () {System.Console.WriteLine ("A roof that opens up."); } } classMinivan:car { Public Override voidDescribeCar () {System.Console.WriteLine ("carries seven people."); } } Public classProgram { Public Static voidMain () {Car car1=NewCar (); Car1. DescribeCar (); System.Console.WriteLine ("----------"); //Four wheels and an engine.
Convertiblecar car2x = new Convertiblecar (); Car2x. DescribeCar (); System.Console.WriteLine ("----------"); A roof that opens up.
New Convertiblecar (); Car2. DescribeCar (); System.Console.WriteLine ("----------"); four wheels and an engine.
Minivan car3x = new minivan (); Car3x. DescribeCar (); System.Console.WriteLine ("----------"); carries seven people.
New
System.Console.WriteLine ("----------"); carries seven people.
Virtual
The adornment method means that these objects are allowed to be overridden in a derived class, and by default the method is non-virtual and cannot be overridden, and the virtual keyword cannot be used with static, abstract, private, override.
The virtual keyword is also closely related to the override, and if you want to implement the virtual method you must use the override or the New keyword (as noted above, the mechanism of new and override is different).
Sealed
When the sealed modifier is applied to a class, this modifier prevents other classes from inheriting from the class.
The sealed method must be used with override, meaning that the parent class of the class that implements the sealed method must implement this method.
A sealed method overrides a method in a base class, but it cannot be overridden in any derived class
classX {protected Virtual voidF () {Console.WriteLine ("X.F"); } protected Virtual voidF2 () {Console.WriteLine ("X.f2"); } } classy:x {Sealed protected Override voidF () {Console.WriteLine ("Y.F"); } protected Override voidF2 () {Console.WriteLine ("x.f3"); } } classZ:y {//attempting to override F causes compiler error CS0239. //protected override void F () {Console.WriteLine ("C.F");}//the method of sealed modification is not allowed to inherit//overriding F2 is allowed. protected Override voidF2 () {Console.WriteLine ("Z.f2"); } }
Overall Example:
namespaceTestvirtualf {InterfaceBaseInterface {voiddoWork (); } Public Abstract classBase:baseinterface { Public Virtual voidWork () {Console.WriteLine ("base class---It's time for work ."); } Public Virtual voidoutwork () {Console.WriteLine ("base class---It's time for work ."); } Public Abstract voidPlay ();//declares an abstract method, only in the abstract method Public Abstract voidDoWork ();//an abstract class that implements an interface that can map an interface method to an abstract method } Public classEmployer:base { Public New voidWork () {Console.WriteLine ("Subclass (NEW)---it's time for work ."); } Public Override voidoutwork () {Console.WriteLine ("Subclass (Override)---it's time off ."); } Public Override voidPlay () {Console.WriteLine ("Subclass (Override)---The parent class abstract method"); } Public Override voidDoWork () {Console.WriteLine ("Parent class abstract method--dowork"); } } classProgram {Static voidMain (string[] args) { /*Employer EMP = new employer (); Emp.work (); Emp.outwork (); Emp.play ();*/ /*Output * Subclass (NEW)---it's time to work * Subclass (Override)---it's time to get off the clock .*/ /*Employer EMP = new employer (); Base B = (base) emp; b.ID = "123"; B.work (); B.outwork (); B.play ();*/ /*Execution Result * Base class---It's time to work * subclass (NEW)---it's time to work * Subclass (Override)---it's time to go now. /c8>*/Base b=NewEmployer (); B.work (); B.outwork (); B.play (); /*Execution Result * Base class---It's time to work * subclass (NEW)---it's time to work * Subclass (Override)---it's time to go now. /c3>*/ } } }
C # onverride, abstract, vitrtual, new, sealed