ArticleDirectory
- Implement multiple interfaces
An interface is a "main class" that contains method signatures but does not implement methods. In this respect, interfaces and abstract classes only contain abstract methods. C # interfaces are very similar to Java interfaces and basically work in the same way.
All members of an interface are defined as public members, and the interface cannot contain constants, fields (private data members), constructors, destructor, or static members of any type. If any modifier is specified for the interface member, the compiler will generate an error.
To implement the interface, we can derived from the interface. Such a derived class must be implemented for all interface methods unless declared as abstract.
The interface declaration is exactly the same as that of Java. In the interface definition, the get and set keywords are used separately. The attribute only indicates its type, and whether it is read-only, write-only, or read-write. The following interface declares a read-only attribute:
1 Public Interface Imethodinterface
2 {
3 // Method signatures
4 Void Methoda ();
5 Int Methodb ( Float Parameter1, Bool Parameter2 );
6
7 // Properties
8 Int Readonlyproperty
9 {
10Get;
11}
12 }
13
14
A colon is used to replace the JAVA Implementation keyword, and the class can inherit this interface. The implementation class must provide the definition of all methods and any required property accessors:
1 Public Class Interfaceimplementation: imethodinterface
2 {
3 // Fields
4 Private Int Count = 0 ;
5 Private Int ID;
6
7 // Implement methods defined in Interface
8 Public Void Methoda ()
9 {
10
11}
12
13 Public Int Methodb ( Float Parameter1, Bool Parameter2)
14 {
15
16ReturnIntegervariable;
17}
18
19 Public Int Readonlyproperty
20 {
21 Get
22 {
23ReturnCount;
24}
25 }
26
27 // Add extra methods if required
28
29 }
30
31
Implement multiple interfaces
By using the following syntax, a class can implement multiple interfaces:
Public Class Myclass: interfacename1, interfacename2, interfacename3
If a class implements multiple interfaces, the member name will have ambiguity. This problem can be solved by using the full qualifier of the attribute or method name. In other words, the derived class can resolve this conflict by using a fully qualified name of the method to indicate which interface it belongs to (for example, imethodinterface. methoda.
Article from: http://www.microsoft.com/china/msdn/library/langtool/vcsharp/Usgettingstartedcsharpforjava.mspx? MFR = true