This article is transferred from: http: // blog. ***/Article. asp? Id = 58
Directory
Interface Definition
Interface and abstract class
Interface implementation
Interface Polymorphism
1. Interface Definition
Definition: defines an agreement. Classes or structures that implement interfaces must comply with their agreements.
Simply put, it is a display definition that is followed when interfaces or classes interact. When I first came into contact with the concept of "interface interaction between classes", I mistakenly thought that interfaces are open methods of classes, and classes interact with each other through class methods. In fact, interfaces are independent from the definition of classes. Interface defines the criteria for interaction between classes.
So it is better to directly interact between classes. Why should I use interfaces?
This is mainly because the interface is an abstraction of the Interaction content between classes. It abstracts the content that requires interaction between classes and defines it as an interface to better control the Logical Interaction between classes. It can be seen that the abstraction of interface content is related to the logic quality of the entire program. In addition, you can add new functions at any time by developing additional interfaces and implementations;
An important concept of an interface is that an interface only contains member definitions and does not contain Member implementations. Member implementations must be implemented in inherited classes or structures.
Interface members include methods, features, indexers, and events.
Note: The interface does not contain fields.
The class that implements the interface must implement every aspect of the interface according to its definition.
Once the API is released, it cannot be changed. Changes to the released API will destroy the existing code.
A typical interface Example:
Using system;
Using system. colletion;
Public Delegate voic Chang (Object sender, object event) // defines a delegate
Public interface ibroker // defines a stock Economic Person Interface
{
String getrating (string stock); // a method for obtaining the amount (not implemented here)
Decimal pricepertrade // defines a property that sets the price per share.
{
Get; // unimplemented
Set;
}
Decimal this (string stockname) // defines the Indexer
{
Get;
Set;
}
Event change pricechange; // event that defines the interface
}
Ii. interfaces and abstract classes
Abstract classes and interfaces have many similarities in definition and function. To use abstract classes or interfaces in a program, you must compare the specific differences between abstract classes and interfaces.
Abstract class: a class that cannot be instantiated but must be inherited from it. abstract classes can be implemented or not implemented.
Subclass can only inherit from one abstract class
Abstract classes are mainly used for closely related objects.
If you want to design a large functional unit, use an abstract class.
If you want to create multiple versions of a component, create an abstract class
Interface: it is a fully abstract set of members and does not provide recognition implementation.
The class or structure can inherit several interfaces.
The interface is most suitable for providing general functions for irrelevant classes.
If you want to design small and concise functional blocks, use the interface
The interface cannot be changed once it is created. If you need a new version of the interface, you must create a new interface
Iii. Implementation of interfaces
Interface implementation includes implicit implementation and explicit implementation. If a class or structure needs to implement a single interface, it can be implemented implicitly. If the class or structure inherits multiple interfaces, the same name members in the interface must be displayed. Display is implemented by fully qualified names of interfaces.
For the above example, we can implement the interface as follows:
Public class testinterface: ibroker // defines a class that inherits the ibroker Interface
{
Hashtable hash = new hashtable ();
Decimal pricepertrade;
Public testinterface (decimal price) // Constructor
{
Pricepertrade = price; // initialization string
}
Public String getrating (string stock) // implicit interface Implementation Method
{
Return "Buy ";
}
Public decimal ibroker. pricepertrade // explicitly implement interface features
{
Get
{
Return pricepertrade;
}
Set
{
Pricepertrade = value;
Pricechange ("finacebroker", value );
}
Public decimal this (string stockname)
{
Get
{
Return (decimal) hash [stockname];
}
Set
{
Hash. Add (stockname, value );
}
}
}
Public event changer pricechange; // all the members in the interface must implement
}
Iv. polymorphism in the interface
Multiple classes inherit the same interface and realize the interface polymorphism. The interface polymorphism access is the same as the class polymorphism access. The following example shows how to implement multi-threaded access to an interface:
Public class interfacetester
{
Public stratic int main (string [] ARGs)
{
String recommendation;
Arraylist brokers = new arraylist; // defines a list.
Brokers. Add (New firstbroker (7.21 m); // Add the class of the first inherited Interface
Brokers. Add (New secondbroker (12.3 m); // Add the class of the second inherited Interface
Interfacettester new iftst = new interfacettester
Foreach (ibroker broker in brokers)
{
Broker. pricechange + = New Change (iftst. pricepertradechange );
Broker ["ADC"] = 12.33 m;
Broker ["rty"] = 11.23
Brokers. pricepertrade = 12.55 m;
}
}
}