- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
object. in Visual C#
object. Data Matrix ECC200 Generation In Visual C# Using Barcode printer for .NET framework Control to generate, create Data Matrix 2d barcode image in .NET framework applications. www.OnBarcode.comDataMatrix Recognizer In Visual C# Using Barcode scanner for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comFigure 17-5 illustrates the duplicate interface methods being implemented by a single class-level method implementation. Encode Code 39 Full ASCII In Visual C# Using Barcode generation for .NET Control to generate, create Code-39 image in .NET applications. www.OnBarcode.comPaint UPC - 13 In C# Using Barcode generator for Visual Studio .NET Control to generate, create European Article Number 13 image in .NET applications. www.OnBarcode.comFigure 17-5. Multiple interfaces implemented by the same class member
QR Code 2d Barcode Drawer In C#.NET Using Barcode creation for .NET Control to generate, create QR image in VS .NET applications. www.OnBarcode.comPrinting Barcode In Visual C#.NET Using Barcode generation for .NET Control to generate, create Barcode image in VS .NET applications. www.OnBarcode.comCHAPTER 17 INTERFACES
Painting 2D Barcode In Visual C# Using Barcode maker for .NET framework Control to generate, create Matrix 2D Barcode image in Visual Studio .NET applications. www.OnBarcode.comLeitcode Printer In C#.NET Using Barcode printer for .NET framework Control to generate, create Leitcode image in VS .NET applications. www.OnBarcode.comReferences to Multiple Interfaces
Data Matrix 2d Barcode Encoder In Objective-C Using Barcode generator for iPhone Control to generate, create ECC200 image in iPhone applications. www.OnBarcode.comData Matrix Creator In Objective-C Using Barcode printer for iPad Control to generate, create DataMatrix image in iPad applications. www.OnBarcode.comYou saw previously that interfaces are reference types, and that you can get a reference to an interface by casting an object reference to the interface type. If a class implements multiple interfaces, you can get separate references for each one. For example, the following class implements two interfaces with the single method PrintOut. The code in Main calls method PrintOut in three ways: Through the class object Through a reference to the IIfc1 interface Through a reference to the IIfc2 interface Figure 17-6 illustrates the class object and references to IIfc1 and IIfc2. interface IIfc1 { void PrintOut(string s); } interface IIfc2 { void PrintOut(string s); } class MyClass : IIfc1, IIfc2 { public void PrintOut(string s) { Console.WriteLine("Calling through: } } // Declare interface // Declare interface // Declare class Barcode Generation In Objective-C Using Barcode drawer for iPhone Control to generate, create Barcode image in iPhone applications. www.OnBarcode.comBarcode Encoder In None Using Barcode drawer for Office Excel Control to generate, create Barcode image in Office Excel applications. www.OnBarcode.com{0}", s); Print Barcode In Java Using Barcode printer for Android Control to generate, create Barcode image in Android applications. www.OnBarcode.comRecognize EAN-13 In Java Using Barcode scanner for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comContinued
Encode Barcode In None Using Barcode encoder for Microsoft Word Control to generate, create Barcode image in Microsoft Word applications. www.OnBarcode.comCreate Code 39 In Java Using Barcode generation for BIRT reports Control to generate, create Code 3/9 image in BIRT reports applications. www.OnBarcode.comCHAPTER 17 INTERFACES
PDF-417 2d Barcode Creation In Visual Basic .NET Using Barcode drawer for .NET framework Control to generate, create PDF 417 image in .NET applications. www.OnBarcode.comCreate QR Code ISO/IEC18004 In Java Using Barcode creation for Java Control to generate, create QR-Code image in Java applications. www.OnBarcode.comclass Program { static void Main() { MyClass mc = new MyClass(); IIfc1 ifc1 = (IIfc1) mc; IIfc2 ifc2 = (IIfc2) mc; mc.PrintOut("object."); ifc1.PrintOut("interface 1."); ifc2.PrintOut("interface 2."); } } This code produces the following output: Calling through: Calling through: Calling through: object. interface 1. interface 2. UPC-A Maker In .NET Framework Using Barcode creator for ASP.NET Control to generate, create UPC-A Supplement 2 image in ASP.NET applications. www.OnBarcode.comCode 128 Code Set A Generator In Java Using Barcode drawer for Android Control to generate, create Code 128B image in Android applications. www.OnBarcode.com// Get ref to IIfc1 // Get ref to IIfc2 // Call through class object // Call through IIfc1 // Call through IIfc2 Figure 17-6. Separate references to different interfaces in the class
CHAPTER 17 INTERFACES
An Inherited Member As an Implementation
A class implementing an interface can inherit the code for an implementation from one of its base classes. For example, the following code illustrates a class inheriting implementation code from a base class. IIfc1 is an interface with a method member called PrintOut. MyBaseClass contains a method called PrintOut that matches IIfc1 s method. Class Derived has an empty declaration body, but derives from class MyBaseClass and contains IIfc1 in its base class list. Even though Derived s declaration body is empty, the code in the base class satisfies the requirement to implement the interface method. interface IIfc1 { void PrintOut(string s); } class MyBaseClass { public void PrintOut(string s) { Console.WriteLine("Calling through: } } class Derived : MyBaseClass, IIfc1 { } class Program { static void Main() { Derived d = new Derived(); d.PrintOut("object."); } } // Declare base class. // Declare the method. {0}", s); // Declare class.
// Create class object // Call method
Figure 17-7 illustrates the preceding code. Notice that the arrow from IIfc1 goes down to the code in the base class. Figure 17-7. Implementation in the base class
CHAPTER 17 INTERFACES
Explicit Interface Member Implementations
You saw in a previous section that a single class can implement all the members required by multiple interfaces, as illustrated in Figures 17-5 and 17-6. But what if you want separate implementations for each interface In this case, you can create what are called explicit interface member implementations. An explicit interface member implementation has the following characteristics: Like all interface implementations, it is placed in the class or struct implementing the interface. It is declared using a qualified interface name, which consists of the interface name and member name, separated by a dot. The following code shows the syntax for declaring explicit interface member implementations. Each of the two interfaces implemented by MyClass implements its own version of method PrintOut. class MyClass : IIfc1, IIfc2 { Qualified interface name void IIfc1.PrintOut (string s) { ... } void IIfc2.PrintOut (string s) { ... } } Figure 17-8 illustrates the class and interfaces. Notice that the boxes representing the explicit interface member implementations are not shown in gray, since they now represent actual code. // Explicit implementation
// Explicit implementation
Figure 17-8. Explicit interface member implementations
CHAPTER 17 INTERFACES
For example, in the following code, class MyClass declares explicit interface member implementations for the members of the two interfaces. Notice that in this example there are only explicit interface member implementations. There is no class-level implementation. interface IIfc1 { void PrintOut(string s); } interface IIfc2 { void PrintOut(string t); } class MyClass : IIfc1, IIfc2 { Qualified interface name void IIfc1.PrintOut(string s) { Console.WriteLine("IIfc1: {0}", s); } Qualified interface name void IIfc2.PrintOut(string s) { Console.WriteLine("IIfc2: {0}", s); } } class Program { static void Main() { MyClass mc = new MyClass(); IIfc1 ifc1 = (IIfc1) mc; ifc1.PrintOut("interface 1."); IIfc2 ifc2 = (IIfc2) mc; ifc2.PrintOut("interface 2."); } } This code produces the following output: IIfc1: IIfc2: interface 1. interface 2. // Declare interface // Declare interface
|
|