Information Technology Reference
In-Depth Information
interface IIfc1 { void PrintOut(string s); } // Declare interface
interface IIfc2 { void PrintOut(string s); } // Declare interface
class MyClass : IIfc1, IIfc2 { // Declare class
public void PrintOut(string s)
{
Console.WriteLine("Calling through: {0}", s);
}
}
class Program {
static void Main() {
MyClass mc = new MyClass();
IIfc1 ifc1 = (IIfc1) mc; // Get ref to IIfc1
IIfc2 ifc2 = (IIfc2) mc; // Get ref to IIfc2
mc.PrintOut("object."); // Call through class object
ifc1.PrintOut("interface 1."); // Call through IIfc1
ifc2.PrintOut("interface 2."); // Call through IIfc2
}
}
This code produces the following output:
Calling through: object.
Calling through: interface 1.
Calling through: interface 2.
Figure 17-6 illustrates the class object with references to the class object, to IIfc1 , and
to IIfc2 .
Figure 17-6. Separate references to different interfaces in the class
Search WWH ::




Custom Search