[Abstract class]
Concept, Three points:
1. abstract classes cannot be instantiated;
2. The subclass must override the abstract method in the parent class;
3. If the class includes abstract methods, the class must be defined as an abstract class, regardless of whether it includes other general methods.
Purpose: abstract classes should be as common as possibleCode, Less data.
Performance:
Keywords: abstract and override.
An abstract method cannot have a method body ending with ";" without "{}".
Different from polymorphism virtual and override. Virtual modification is not necessarily a method. Except for fields, attributes, events, and indexers can all be virtual, and methods have method bodies, subclasses do not have to override the methods and attributes of their parent classes. Abstract fields cannot be abstracted. Other fields can be abstracted.
Features:
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 that the abstract 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 a method or attribute does not contain an implementation.
Abstract methods have the following features:
The abstract method is an implicit virtual method.
Only abstract methods can be declared in abstract classes.
The abstract method declaration does not provide actual implementation, so there is no method body. The method declaration ends with a semicolon and there is no braces ({}) after the signature ({}).
The implementation is provided by an override method, which is a non-abstract class member.
It is incorrect to use the static or virtual modifier in the abstract method declaration.
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 class, you can override abstract inheritance attributes by including the attribute declaration using the override modifier.
Abstract classes must be implemented for all interface members.
Abstract classes that implement interfaces can map interface methods to abstract methods.
Example:
using system;
abstract class baseclass
{< br> protected int _ x = 100;
protected int _ y = 150;
public abstract void abstractmethod ();
public abstract int X {Get ;}< br> public abstract int y {Get ;}
}< br>
class derivedclass: baseclass
{< br> Public override void abstractmethod ()
{< br> _ x ++;
_ y ++;
}< br>
Public override int x
{< br> Get
{< br> return _ x + 10;
}< BR >}< br>
Public override int Y
{< br> Get
{< br> return _ y + 10;
}< BR >}< br>
static void main ()
{< br> derivedclass o = new derivedclass ();
O. abstractmethod ();
console. writeline ("x = {0}, y = {1}", O. x, O. y);
}< BR >}