C # abstract classes and abstract methods,
Overview: abstract keywords abstract classes cannot be instantiated, abstract methods cannot have method bodies, and all abstract methods in abstract classes must be rewritten in subclass (Override). An abstract class can contain both abstract and non-abstract methods.
Abstract Modifiers can be used with classes, methods, attributes, indexers, and events.
Use in class declarationAbstractModifier to indicate that the class can only be the base class of other classes.
Abstract classes have the following features:
- Abstract classes cannot be instantiated.
- Abstract classes can contain abstract methods and abstract accessors.
- The abstract class cannot be modified with the sealed modifier, which means the class cannot be inherited.
- A non-abstract class derived from an abstract class must include all the inherited abstract methods and the actual implementation of the abstract accessors.
Used in method or attribute DeclarationAbstractModifier to indicate that this method or attribute does not contain an implementation.
Abstract methods have the following features:
Except for the differences in the declaration and call syntax, abstract attributes behave the same way as abstract methods.
- Use static attributesAbstractThe modifier is incorrect.
- In a derived classOverrideThe modifier's attribute declaration can override abstract inherited attributes.
Abstract classes must be implemented for all interface members.
In MSDN, the C # language standard is: 10.1.1.1 abstract class. This article is as follows:
abstract
The modifier is used to indicate that the modified class is incomplete and can only be used as the base class. Abstract classes and non-abstract classes are different in the following aspects:
- Abstract classes cannot be directly instantiated and used for abstract classes
new
The operator is a compilation error. Although some types of variables and values during compilation can be abstract, such variables and values must ornull
Or include references to instances that are not abstract classes (this non-abstract class is derived from the abstract class ).
- Allowed (but not required) abstract classes include abstract members.
- Abstract classes cannot be sealed.
When a non-abstract class is derived from an abstract class, these non-abstract classes must specifically implement all the abstract members inherited to override those abstract members. In the following example
abstract class A { public abstract void F(); } abstract class B: A { public void G() {} } class C: B{public override void F() {// actual implementation of F}}
Abstract classA
Introduce Abstract METHODSF
. ClassB
Introduce another methodG
But since it does not provideF
Implementation,B
It must also be declared as an abstract class. ClassC
RewriteF
And provides a specific implementation. BecauseC
No abstract member in, so you can (but not required)C
The Declaration is not an abstract class.
So when should we use abstract classes?
If the object of a class design is inherited by other classes and it represents the public attributes or methods of A Class Object, the class should be set as an abstract class.