C # interfaces, abstraction, and delegation in traditional impressions

Source: Internet
Author: User

Before learning the new features of C #2.0, let's take a look at the interface, abstraction, and delegation in C #'s traditional impressions.
1. interfaces include a series of declarations such as methods and attribute definitions. These interfaces must be implemented using classes that implement interfaces.
Interface Definition:
[Access permission] Interface Name
{
Interface body;
}
Common Access Permissions for interfaces include public or internal.
The interface body defines the code items that must be provided for various implementation classes. Interfaces can define methods, attributes, indexes, and events, but do not include fields.
For example, operations on all vehicles include "start" and "stop". The status is only running and non-running. We can use the START () and stop () Methods to model these functions, "started" is used to indicate whether a vehicle is running or not. For example:
Public interface idrivable
{
Void start ();
Void stop ();
Bool started
{
Get;
}
}
The interface only standardizes the get attribute of started and the two operation functions of the vehicle, and returns the void type.
The following class implements this interface:
Public class car: idrivable
{
Private bool _ started = false;
Public void start ()
{
_ Started = true;
}
Public void stop ()
{
_ Started = false;
}
Public bool started
{
Get
{
_ Return started;
}
}
}

Next, the program will be able to operate the vehicle by instantiating the car class:
Car mycar = new car ();
Mycar. Start ();

2. abstract classes have the following features)
Declare an abstract method using abstract keywords;
A class can contain one or more abstract methods;
Abstract classes can have non-abstract methods;
Abstract classes cannot be directly instantiated;
The abstract class is implemented using ":" (colon), and the abstract method is implemented using the override keyword;
Abstract classes can be inherited by abstract classes, and the results are still abstract classes;
After an abstract method is implemented, the modifier cannot be modified and must be overloaded before use ).

For example:
Public abstract class person
{
Public abstract void sayhello (); // abstract Method
Public void about () // non-abstract Method
{
Console. writeline ("abstract Demo ");
}
}

Public class student: person
{
Public override void sayhello () // overload of the abstract Method
{
Console. writeline ("sayhello ");
}
}
Class mainclass
{
Public static void main ()
{
New student (). sayhello (); // use the overloaded class and Method
}
}

3. Delegate (the original text is from the Internet, which is more thorough than me, so I will directly cite it. Thanks to the original author)
The concepts of delegation and event fully cooperate. The delegate is just a function pointer, that is, it can reference the function and complete it through the address transfer mechanism.
A delegate is a class. When you instantiate It, You need to provide a reference function and use it as a parameter of its constructor.
Each delegate has its own signature, for example: Delegate int somedelegate (string S, bool B); is a delegate declaration, here, the signature mentioned,
That is to say, the somedelegate delegate has the string and bool type parameters and returns an int type.
As mentioned above: When you instantiate a delegate, you need to provide a reference function and use it as a parameter of its constructor.
Note: The referenced function must have the same signature as the delegate.
 
Take a look at the following functions:
Private int somefunction (string STR, bool BLN ){...}

You can pass this function to the somedelegate constructor because they have similar signatures (in other words, they all have the same parameter type and number, and return the same data type ).

Somedelegate SD = new somedelegate (somefunction );

SD references somefunction. That is to say, somefunction has been registered by SD. If you call SD, somefunction will also be called,
Remember: the meaning of somefunction, which we will use later.

A delegate is similar to a function pointer. It can reference both static methods and instance methods.
Delegation is divided into three steps: 1. Delegation statement. 2. Delegate instantiation. 3. Delegate call.

Example:

Using system;

Namespace Delegation
{
Delegate int numope (int A, int B); // delegate statement
Class class1
{
Static void main (string [] ARGs)
{
Class1 C1 = new class1 (); # P # page title # e #
Numope p1 = new numope (c1.add); // delegate instantiation
Console. writeline (p1 (1, 2); // delegate call
Console. Readline ();
}

Private int add (INT num1, int num2)
{
Return (num1 + num2 );
}
}
}

In this example, the numope delegate references the add method.
After the delegate is declared, it can be instantiated like a class. During the instantiation, the referenced method (such as: add) is used as a parameter, so that the delegate and method are associated, you can use the delegate to reference the method.
The delegate and the referenced method must be consistent: the number, type, and order of parameters must be consistent, and the return value must be consistent.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.