C # inheritance, abstract classes, sealed classes, and class Polymorphism
Classes can be inherited from other classes.
The new class (that is, the derived class) will obtain all non-private data and behaviors of the base class and all other data or behaviors defined by the new class for itself. Therefore, the new class has two valid types: the type of the new class and the type of the class it inherits.
In the above example, Class B is both valid B and valid. When accessing object B, you can use the forced conversion operation to convert it to object. Forced conversion does not change B object, but your B object view limits the data and behavior to. After B is forcibly converted to a, the can be forcibly converted to B. Not all instances of A can be forcibly converted to B. Only instances of B can be forcibly converted to B. If Class B is used as Class B, data and behavior of Class A and Class B can be obtained at the same time. Objects can represent multiple types of capabilities called polymorphism. For more information, see polymorphism (C # programming guide ). For more information about force conversion, see force conversion (C # programming guide ).
{
Expandcollapse (sectiontoggle0)
} "Onkeypress =" function onkeypress ()
{
Expandcollapse_checkkey (sectiontoggle0)
} ">Abstract classes and Class Members
UseAbstractKeywords can be used to create classes and class members for the purpose of inheritance only, that is, to define the function of a derived non-abstract class. You can use the sealed keyword to avoid inheriting the class or some class members previously marked as virtual. For more information, see How to: Define abstract attributes (C # programming guide ).
PublicAbstractClass A {// 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.
Abstract classes can also define abstract methods. The method is to add the abstract keyword to the front of the return type of the method. For example:
PublicAbstractClass A {public abstract void dowork (int I );}
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 Class D
{
Public Virtual void dowork (int I)
{
// Original implementation.
}
}
Public abstract class E: d
{
Public abstract override void dowork (int I );
}
Public Class F: E
{
Public override void dowork (int I)
{
// New implementation.
}
}
If you declare a virtual method as an abstract method, it is still virtual for all classes inherited from the abstract class. Classes that inherit abstract methods cannot access the original implementation of this method. In the previous example, dowork on Class F cannot call dowork on Class D. In this case, the abstract class can force the derived class to provide a new method implementation for the virtual method.
Sealing Class and Class Member
You can declare a class as a sealed class. The method is to place the keyword sealed before the keyword class in the class definition. For example:
PublicSealedClass D {// Class Members here .}
The sealing class cannot be used as the base class. Therefore, it cannot be an abstract class. The seal class is mainly used to prevent derivation. 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 before the override keyword in the class member declaration. For example:
Public Class D: c {publicSealedOverride void dowork (){}}
Summary of PolymorphismThrough inheritance, a class can be used as multiple types: it can be used as its own type, any base type, or any interface type when implementing an interface. This is known as polymorphism. The State is not only important to the derived class, but also to the base class. The designer of the base class needs to predict possible changes. When the derived class inherits from the base class, it obtains all methods, fields, attributes, and events of the base class. To change the data and behavior of the base class, you have two options:
Replace a base Member with a new derived Member, Or you can
Override virtual base members. To replace a base class member with a new derived member, you must use
NewKeyword. If the base class defines a method, field, or attribute, the new keyword is used to create a new definition of the method, field, or attribute in the derived class. The new keyword is placed before the return type of the class member to be replaced. For example:
Public class baseclass
{
Public void dowork (){}
Public int workfield;
Public int workproperty
{
Get {return 0 ;}
}
}
Public class derivedclass: baseclass
{
Public new void dowork (){}
Public new int workfield;
Public new int workproperty
{
Get {return 0 ;}
}
}
When the new keyword is used, the new class member is called instead of the replaced base class member. These base class members are called hidden members. If you forcibly convert an instance of a derived class to an instance of the base classStill OKCall to hide class members. For example:
Derivedclass B = new derivedclass ();
B. dowork (); // callthe new method.
Baseclass A = (baseclass) B;
A. dowork (); // callthe old method.
In order for the instance of the derived class to completely replace the class member from the base class, the base class must declare the member as virtual. This is done by addingVirtualKeyword. Then, you can choose to use the derived classOverrideKeyword instead of new, replace the base class implementation with its own implementation. For example:
Fields cannot be virtual. Only methods, attributes, events, and indexes can be virtual. When a derived class overrides a virtual member,This member is called even if the instance of the derived class is accessed as the base class instance.. For example:
You can use virtual methods and attributes to pre-plan future extensions. Because the type used by the call founder is not considered when calling a virtual member, the derived class can choose to completely change the appearance of the base class.
No matter how many classes have been declared between the derived class and the class that originally declares a virtual member, the virtual member will always be a virtual member. If Class A declares a virtual Member, Class B is derived from Class A, and class C is derived from Class BClass C inherits the Virtual Member and can choose to override it, regardless of whether Class B declares the rewrite for the member. For example:
A derived class can stop virtual inheritance by declaring the override as sealed. In the class member declarationSealedKeyword placementOverrideBefore the keyword. For example
Public Class C: B {public sealed override void dowork (){}}
In the preceding example, the method dowork is no longer virtual to any class derived from C. It is still virtual for instances of C-even if these instances are forcibly converted to type B or type. You can use the new keyword to replace the seal of a derived class, as shown in the following example:
Public Class D: c {public new void dowork (){}}
In this case, if you use a variable of the D type in D to call dowork, the new dowork will be called. If a variable of type C, B, or a is used to access the instance of D, the call to dowork follows the rules of virtual inheritance, that is, these calls are transmitted to the dowork Implementation of class C.
A derived class that has replaced or overwritten a method or attribute can still use the base keyword.BaseAccess this method or attribute of the base class. For example:
BaseKeyword is used to access a member of the base class from a derived class:
Call methods that have been overwritten by other methods on the base class.
Specify the base class constructor to call when creating a derived class instance.
Base Class access can only be performed in constructors, instance methods, or instance attribute accessors.
It is incorrect to use the base keyword in a static method.
Public Class
{
Public Virtual void dowork (){}
}
Public Class B:
{
Public override void dowork (){}
}
Public Class C: B
{
Public override void dowork ()
{
// Call dowork on B to get B's behavior:
Base. dowork ();
// Dowork behavior specific to C goes here:
//
}
}
Public Class
{
Public Virtual void dowork (){}
}
Public Class B:
{
Public override void dowork (){}
}
Public Class C: B
{
Public override void dowork (){}
}
Derivedclass B = new derivedclass ();
B. dowork (); // callthe new method.
Baseclass A = (baseclass) B;
A. dowork (); // also callthe new method.
Public class baseclass
{
Public Virtual void dowork (){}
Public Virtual int workproperty
{
Get {return 0 ;}
}
}
Public class derivedclass: baseclass
{
Public override void dowork (){}
Public override int workproperty
{
Get {return 0 ;}
}
}
Public Class
{
Public (){}
}
Public Class B:
{
Public B (){}
}