1. abstract classes and Class Members
You can use the abstract keyword to create incomplete classes and class members that must be implemented in the derived class.
For example:
PublicAbstractClassA
{
// Class members here.
}
Abstract classes cannot be instantiated. Abstract classes are used to provide public definitions of the base classes that can be shared by multiple Derived classes. For example, a class library can define an abstract class as a parameter of multiple of its functions, and requiresProgramYou can use this library to create a derived class to provide your own class implementation.
Abstract classes can also define abstract methods. The method is to set the keywordAbstractAdd to the front of the return type of the method.
For example:
PublicAbstractClassA
{
PublicAbstractVoidDowork (IntI );
}
Abstract methods are not implemented, so the method definition is followed by a semicolon instead of a regular method block. The derived class of the abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, the abstract class can use the abstract method to override the virtual method.
For example:
// Compile with:/Target: Library
Public ClassD
{
PublicVirtualVoidDowork (IntI)
{
// Original implementation.
}
}
PublicAbstractClassE: d
{
PublicAbstract overrideVoidDowork (IntI );
}
Public ClassF: E
{
PublicOverrideVoidDowork (IntI)
{
// New implementation.
}
}
IfVirtualThe method declaration isAbstractThis method is still a virtual method for all classes inherited from the abstract class. Classes that inherit abstract methods cannot access the original implementation of this method. In the previous exampleDoworkCannot callDowork. In this case, the abstract class can force the derived class to provide a new method implementation for the virtual method.
2. Sealing Class and Class Members
You can use the sealed keyword to avoid inheriting the class or some class members previously marked as virtual.
For example:
PublicSealedClassD
{
// Class members here.
}
The sealing class cannot be used as the base class. Therefore, it cannot be an abstract class. The seal class cannot be derived. Since the seal class is never used as the base class, some runtime optimization can make the call to the seal class members slightly faster.
The class member, method, field, attribute, or event on the derived class that overwrites the Virtual Member of the base class can be declared as a sealed member. When used in a later derived class, this will cancel the virtual effect of the member. The method is to place the sealed keyword in the class member declaration
Before the override keyword. For example:
Public ClassD: c
{
PublicSealed overrideVoidDowork (){}
}
See http://msdn.microsoft.com/zh-cn/library/kx37x362.aspx