abstract and virtual in C#

Re-understand the keyword abstract and make the following summary:

1. Classes marked as abstract cannot be instantiated, but they can still have constructors, or overload constructors, and call them in subclasses

2. The abstract class may or may not have the methods and properties of the abstract mark, and the marked methods and properties cannot be implemented

3. If the subclass is not marked as abstract, it must use override to implement the methods and properties marked as sabtract in the abstract parent class

important point:

1. The method marked as abstract can no longer be modified with static or virtual modifiers

2. The abstract modifier cannot be used on static properties

 

 3. After the abstract tag class inherits the interface, it must implement the interface method, or after it is marked as abstract, it must be implemented by the subclass

common ground:

1. Both virtual and abstract can be overridden by subclasses, using the override keyword

2. Neither can be marked as private

difference:

1. The abstract method can only be declared in the abstract class, not in the ordinary class, and the virtual method has no restrictions

2. The abstract method cannot have a method body, and virtual must have a method body, even if there is a {}

3. Subclasses must implement the abstract method, but virtual is not necessary

Guess you like

Origin blog.csdn.net/yunxiaobaobei/article/details/129366608