C # language is worth learning a lot. Here we mainly introduce the C # Abstract modifier, including the introduction that a member must be implemented in a forced inheritance class. .
C # What does abstract modifier mean?
C # Abstract modifiers can be used for classes, methods, attributes, events, and index indicators (Indexer ), abstract members cannot be declared as abstract members together with static and virtual abstract members, but as long as there are still unimplemented abstract members (that is, abstract classes) in the class ), then its object cannot be instantiated. It is usually used to force the inheritance class to implement a certain member.
Example:
- Using system;
- Using system. Collections. Generic;
- Using system. text;
- Namespace example04
- {
- # Region base class, abstract class
- Public abstract class baseclass
- {
- // Abstract attribute. The get and set accessors indicate that the inherited class must implement the attribute as read/write.
- Public abstract string attribute
- {
- Get;
- Set;
- }
- // Abstract method. Input a string parameter without returning a value
- Public abstract void function (string value );
- // Abstract event. The type is the predefined system proxy (delegate): eventhandler.
- Public abstract event eventhandler event;
- // Abstract index indicator. Only get accessors indicate that the index indicator must be read-only for the inherited class.
- Public abstract char this [int Index]
- {
- Get;
- }
- }
- # Endregion
- # Region inheritance class
- Public class deriveclass: baseclass
- {
- Private string attribute;
- Public override string attribute
- {
- Get
- {
- Return attribute;
- }
- Set
- {
- Attribute = value;
- }
- }
- Public override void function (string value)
- {
- Attribute = value;
- If (event! = NULL)
- {
- Event (this, new eventargs ());
- }
- }
- Public override event eventhandler event;
- Public override char this [int Index]
- {
- Get
- {
- Return attribute [Index];
- }
- }
- }
- # Endregion
- Class Program
- {
- Static void onfunction (Object sender, eventargs E)
- {
- For (INT I = 0; I<(Deriveclass) sender). Attribute. length; I ++)
- {
- Console. writeline (deriveclass) sender) [I]);
- }
- }
- Static void main (string [] ARGs)
- {
- Deriveclass tmpobj = new deriveclass ();
- Tmpobj. Attribute = "1234567 ";
- Console. writeline (tmpobj. Attribute );
- // Associate the static function onfunction with the event of the tmpobj object
- Tmpobj. event + = new eventhandler (onfunction );
- Tmpobj. function ("7654321 ");
- Console. Readline ();
- }
- }
- }