C # Interface
C # Interface
Contents
Interface Definition
Interface and an abstract class
Interface Implementation
Interface polymorphism
1. Interface Definition
Definition: a definition of the Agreement. Implementation of the interface type or structure must comply with their agreements.
It is a simple interface between interactive category, or to comply with the definition of a show. Initial contact "with the interface between interactive type" concept, the misconception that category is open interface methods between the adoption of the method of interaction. In fact interface is independent of the kind of a definition. Interface Interaction between the definition of the standard.
So with the type of direct interaction between like, why use interface?
This is mainly because of the following: the interface between interactive content of an abstract, the type of interaction is needed between the abstract content of 10% by definition interface, can better control the logic of interaction between the category. This shows that interface to the abstract quality of the content related to the quality of the logic of the whole process; another may at any time through the development and the achievement of additional interfaces to add new features;
On the interface a very important concept: Interface include only members of definition does not include members of the realization of the need to achieve the type or structure of succession to achieve.
Interface members include: methods, properties, indexing, incident.
NOTE: interface does not contain field.
Implementation of the interface definition must be strictly according to achieve in every aspect of interface
Once the interface itself can no longer be published changes to the published interface changes would undermine the existing code.
A typical interface examples:
Using System;
Using System.Colletion;
Public delegate voic Chang (object sender, object event) / / define a commissioned
Public interface Ibroker / / definition of a stock Economic Man Interface
(
String GetRating (string stock); / / amount of an access method (here did not materialize)
Decimal pricePerTrade / / define a set of share prices
(
Get; / / unfulfilled
Set;
)
Decimal this (string StockName) / / definition of Index
(
Get;
Set;
)
Event Change pricechange; / / definition of interface events
)
2. Interface and an abstract class
Abstract classes and interfaces in the definition and functions there are many similarities, in the specific procedures used in the abstract class or interface needs more abstract interface type and the specific differences.
Abstract categories: a must and can not be instantiated from the class inheritance, abstract class can achieve, can choose not to provide realization
Subclass can only inherit from an abstract category
Abstract class should be the main target for close
If we design a functional unit, the use of abstract classes.
If the component is expected to create multiple versions, the creation of an abstract class
Interface: members is entirely abstract set, not to provide understanding to achieve.
Type or several interface structure can be inherited.
Interface is not the most suitable for the relevant class provides common functionality
If small and concise design function blocks, the use of interface
Once created on the interface can not be changed. If you need a new version of interface, the need to create a new interface
3. Interface Implementation
Interface Implementation is divided into: implicit and explicit realization achieve. If the type or structure to achieve a single interface, you can use the implicit realized, or if the structure of a number of interface inheritance as members of the same name in the interface to show that realization. Showing realized through the use of the interface to achieve a fully qualified member of the interface.
For the example above, we can achieve this interface:
Public class testInterface: Ibroker / / define a succession of ibroker Interface
(
Hashtable hash = new hashtable ();
Decimal pricepertrade;
Public testInterface (decimal price) / / constructor function
(
Pricepertrade = price; / / initialization string
)
Public string Getrating (string stock) / / implicit realization of the method interface
(
Return "buy";
)
Public decimal Ibroker.pricepertrade / / Explicit realization of the interface
(
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; / / interface of all the members must achieve
)
4. In the multi-state interface
Inherited many of the same interface to achieve polymorphism of the interface, the interface polymorphism of the visit and the visit to the same polymorphism. The following examples illustrate how the interface to achieve polymorphism visit:
Public class InterfaceTester
(
Public stratic int Main (string [] args)
(
String recommendation;
Arraylist Brokers = new Arraylist; / / definition of a list
Brokers.add (new firstBroker (7.21m ));// addition of a successor interface category
Brokers.add (new secondBroker (12.3m ));// adding a second type of interface successor
InterfacetTester new Iftst = new InterfacetTester
Foreach (Ibroker broker in Brokers)
(
Broker.pricechange + = new change (iftst.pricepertradechange);
Broker [ "adc"] = 12.33m;
Broker [ "rty"] = 11.23ml
Broker.pricepertrade = 12.55m;
)
)
)








0 Comments to “C # Interface”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.