C # Access Interface

Author:Anonymous    Updated:2008-3-4 12:11:23
Access Interface


Members of the visit of the interface


On the interface method calls and visits by indexing indicator of the rules of the situation and is the same. If the bottom of the naming and inheritance from the senior members of the same, then the bottom will cover members of the senior members of the same name. However, as interfaces support multiple inheritance, multiple inheritance in, if the father of two interface with the members of the same name, which had a two-yi (This is also abolished the C # inheritance mechanism of multi-one of the reasons why) , then the need for explicit definition:





Using System;
(Interface ISequence
Int Count (get; set;)
)
(Interface IRing
Void Count (int i);
)
Interface IRingSequence: ISequence, IRing ()
(Class CTest
Void Test (IRingSequence rs) (
/ / Rs.Count (1); mistake, there are two justice of the Count
/ / Rs.Count = 1; mistake, there are two justice of the Count
((ISequence) rs). Count = 1; / / correct
((IRing) rs). Count (1); / / correct call IRing.Count
)
)


In the example above, the former two sentences rs. Count (1) and rs. Count = 1 would produce justice, thus leading to compile-time error, and therefore must be explicitly assigned to rs father interface types, such assignment operation will not give rise to additional expenses.


Look at the following example:


Using System;
(Interface IInteger
Void Add (int i);
)
(Interface IDouble
Void Add (double d);
)
Interface INumber: IInteger, IDouble ()
(Class CMyTest
Void Test (INumber Num) (
/ / Num.Add (1); wrong
Num.Add (1.0); / / correct
((IInteger) n). Add (1); / / correct
((IDouble) n). Add (1); / / correct
)
)


Call Num.Add (1) would lead to ambiguity, as the candidate of the heavy-duty type of the parameter methods are applicable. However, the call Num.Add (1.0) is allowed, because 1.0 is the type float parameters and methods IInteger.Add () parameter type of inconsistency, then only IDouble.Add is applicable. However, as long as the accession to the explicit assignment, it will not produce ambiguity.


Multiple interface will also have the question of succession to members of the question. For example:


(Interface IBase
Void FWay (int i);
)
Interface ILeft: IBase (
New void FWay (int i);
)
Interface IRight: IBase
(Void G ();)
Interface IDerived: ILeft, IRight ()
(Class CTest
Void Test (IDerived d) (
D. FWay (1) / / call ILeft. FWay
((IBase) d). FWay (1) / / call IBase. FWay
((ILeft) d). FWay (1) / / call ILeft. FWay
((IRight) d). FWay (1) / / call IBase. FWay
)
)


On the case, in the derivative method IBase.FWay interface ILeft Ileft was a member of the methods FWay coverage. Therefore d. FWay (1) call actually deployed. Although IBase-> IRight-> IDerived path of the succession, ILeft.FWay method is not covered. As long as we keep this in mind: Once the members of coverage after all their visits have been a member of coverage after "intercepting".








Implementation of the interface


We have said before, the interface definition does not include the realization of the method. Interface type or structure can be achieved. We passed on the main categories to achieve interface. Categories used to achieve interface, the interface name must be included in the definition of the base class list.


The following example is given by the class to achieve interface examples. ISequence which interface to a queue, queue provided to the members of the tail add an object methods Add (), a revolving table for IRing interface, to provide a ring inserted in the method of object Insert (object obj), the method returns the location of insertion . RingSquence realization of the interface and the interface ISequence IRing.


Using System;
(Interface ISequence
Object Add ();
)
(Interface ISequence
Object Add ();
)
(Interface IRing
Int Insert (object obj);
)
Class RingSequence: ISequence, IRing
(
Public object Add () ()…
Public int Insert (object obj) ()…
)


If a class implements interface category also implicit inherited the father of all the interface interface, whether they have the father in the interface definition of the base class listed in the table. Look at the following example:


Using System;
(Interface IControl
Void Paint ();
)
Interface ITextBox: IControl (
Void SetText (string text);
)
Interface IListBox: IControl (
Void SetItems (string [] items);
)
Interface IComboBox: ITextBox, IListBox ()


Here, interface IcomboBox inherited ItextBox and IlistBox. Class TextBox ITextBox not realize the interface, but also realized the interface ITextBox father interface IControl.


We have already seen earlier, a class can be achieved multiple interfaces. Look at the following example:


(Interface IDataBound
Void Bind (Binder b);
)
Public class EditBox: Control, IControl, IDataBound (
Public void Paint ();
Public void Bind (Binder b) {...}
)


Class EditBox derived from the category of Control and realized Icontrol and IdataBound. In the case of the interface in front of the Paint Icontrol IdataBound interface methods and the methods used Bind category EditBox members of the public realize. C # provide a method of achieving these alternative channels, this could make the implementation of these class members to avoid these settings for the public. Interface members of the name can be used effectively to achieve. For example, the category EditBox method can be converted to Icontrol.Paint and IdataBound.Bind to achieve.


Public class EditBox: IControl, IDataBound (
Void IControl.Paint () {...}
Void IDataBound.Bind (Binder b) {...}
)


Because members through external interface has assigned each member, so use this method as a member of the external interface members. External interfaces members can call only through the interface. For example, the Paint EditBox method can be realized only through the creation of Icontrol interface to call.


Class Test (
Static void Main () (
EditBox editbox = new EditBox ();
Editbox.Paint (); / / error: Paint the incident did not EditBox
IControl control = editbox;
Control.Paint (); / / Paint the incident call EditBox
)
)


On the case, the category EditBox inherited from the Control class and at the same time achieving the IControl and IDataBound interface. EditBox in Paint from IControl interface method, Bind method from IDataBound interface, in the two categories are as EditBox members of the public realize. Of course, in C #, we can also choose not achieve as a member of the public interface.


If each member is clearly that the interface had been achieved through the realization of this approach was the interface we call explicit interface member (explicit interface member). In this way we rewrite the example above:


Public class EditBox: IControl, IDataBound (
Void IControl.Paint () ()…
Void IDataBound.Bind (Binder b) ()…
)


Explicit interface members only through the interface call. For example:


(Class CTest
Static void Main () (
EditBox editbox = new EditBox ();
Editbox.Paint (); / / error: different methods
IControl control = editbox;
Control.Paint () / / Call the Paint method EditBox
)
)


In the above code editbox.Paint () call is wrong, because the editbox itself does not provide this method. Control.Paint () call is the right way.


Notes: provide the interface itself is not a member of the realization of the definition, it just these members, these members must rely on the realization of the interface type, or other interface support.


Know how to access interface, we have to know how to achieve interface, in order to achieve C # interface, see the following section - the interface







Implementation of the interface


We have said before, the interface definition does not include the realization of the method. Interface type or structure can be achieved. We passed on the main categories to achieve interface. Categories used to achieve interface, the interface name must be included in the definition of the base class list.


The following example is given by the class to achieve interface examples. ISequence which interface to a queue, queue provided to the members of the tail add an object methods Add (), a revolving table for IRing interface, to provide a ring inserted in the method of object Insert (object obj), the method returns the location of insertion . RingSquence realization of the interface and the interface ISequence IRing.


Using System;
(Interface ISequence
Object Add ();
)
(Interface ISequence
Object Add ();
)
(Interface IRing
Int Insert (object obj);
)
Class RingSequence: ISequence, IRing
(
Public object Add () ()…
Public int Insert (object obj) ()…
)


If a class implements interface category also implicit inherited the father of all the interface interface, whether they have the father in the interface definition of the base class listed in the table. Look at the following example:


Using System;
(Interface IControl
Void Paint ();
)
Interface ITextBox: IControl (
Void SetText (string text);
)
Interface IListBox: IControl (
Void SetItems (string [] items);
)
Interface IComboBox: ITextBox, IListBox ()


Here, interface IcomboBox inherited ItextBox and IlistBox. Class TextBox ITextBox not realize the interface, but also realized the interface ITextBox father interface IControl.


We have already seen earlier, a class can be achieved multiple interfaces. Look at the following example:


(Interface IDataBound
Void Bind (Binder b);
)
Public class EditBox: Control, IControl, IDataBound (
Public void Paint ();
Public void Bind (Binder b) {...}
)


Class EditBox derived from the category of Control and realized Icontrol and IdataBound. In the case of the interface in front of the Paint Icontrol IdataBound interface methods and the methods used Bind category EditBox members of the public realize. C # provide a method of achieving these alternative channels, this could make the implementation of these class members to avoid these settings for the public. Interface members of the name can be used effectively to achieve. For example, the category EditBox method can be converted to Icontrol.Paint and IdataBound.Bind to achieve.


Public class EditBox: IControl, IDataBound (
Void IControl.Paint () {...}
Void IDataBound.Bind (Binder b) {...}
)


Because members through external interface has assigned each member, so use this method as a member of the external interface members. External interfaces members can call only through the interface. For example, the Paint EditBox method can be realized only through the creation of Icontrol interface to call.


Class Test (
Static void Main () (
EditBox editbox = new EditBox ();
Editbox.Paint (); / / error: Paint the incident did not EditBox
IControl control = editbox;
Control.Paint (); / / Paint the incident call EditBox
)
)


On the case, the category EditBox inherited from the Control class and at the same time achieving the IControl and IDataBound interface. EditBox in Paint from IControl interface method, Bind method from IDataBound interface, in the two categories are as EditBox members of the public realize. Of course, in C #, we can also choose not achieve as a member of the public interface.


If each member is clearly that the interface had been achieved through the realization of this approach was the interface we call explicit interface member (explicit interface member). In this way we rewrite the example above:


Public class EditBox: IControl, IDataBound (
Void IControl.Paint () ()…
Void IDataBound.Bind (Binder b) ()…
)


Explicit interface members only through the interface call. For example:


(Class CTest
Static void Main () (
EditBox editbox = new EditBox ();
Editbox.Paint (); / / error: different methods
IControl control = editbox;
Control.Paint () / / Call the Paint method EditBox
)
)


In the above code editbox.Paint () call is wrong, because the editbox itself does not provide this method. Control.Paint () call is the right way.


Notes: provide the interface itself is not a member of the realization of the definition, it just these members, these members must rely on the realization of the interface type, or other interface support.
Previous:C # dealing with a text file
Next:C # Interface Converter
User Reviews
Related Articles
Recommended article
AD