Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

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)

// Explicit implementation

 

{ ... }

 

 

 

 

void IIfc2.PrintOut

(string s)

// Explicit implementation

}

{ ... }

 

 

 

 

 

 

 

 

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.

Figure 17-8. Explicit interface member implementations

427

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); }

 

// Declare interface

interface IIfc2 { void PrintOut(string t); }

 

// Declare interface

class MyClass : IIfc1, IIfc2

 

 

 

{

Qualified interface name

 

 

 

 

 

 

 

 

 

 

void IIfc1.PrintOut(string s)

 

// Explicit interface member

 

{

 

 

 

//

implementation

 

Console.WriteLine("IIfc1:

{0}", s);

 

 

 

}

Qualified interface name

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

void IIfc2.PrintOut(string s)

 

// Explicit interface member

 

{

 

 

 

//

implementation

 

Console.WriteLine("IIfc2:

{0}", s);

 

 

}

}

 

 

 

 

 

 

 

 

 

 

 

class Program

 

 

 

{

static void Main()

 

 

 

 

 

 

 

 

{

 

 

 

 

 

 

MyClass mc = new MyClass();

 

// Create class object

 

IIfc1 ifc1 = (IIfc1) mc;

 

// Get reference to IIfc1

 

ifc1.PrintOut("interface 1");

// Call explicit implementation

 

IIfc2 ifc2 = (IIfc2) mc;

 

// Get reference to IIfc2

 

ifc2.PrintOut("interface 2");

// Call explicit implementation

 

}

 

 

 

 

 

}

 

 

 

 

 

 

This code produces the following output:

IIfc1: interface 1

IIfc2: interface 2

428

CHAPTER 17 INTERFACES

Figure 17-9 illustrates the code. Notice in the figure that the interface methods are not pointing at class-level implementations but contain their own code.

Figure 17-9. References to interfaces with explicit interface member implementations

When there is an explicit interface member implementation, a class-level implementation is allowed but not required. The explicit implementation satisfies the requirement that the class or struct must implement the method. You can therefore have any of the following three implementation scenarios:

A class-level implementation

An explicit interface member implementation

Both a class-level and an explicit interface member implementation

429

CHAPTER 17 INTERFACES

Accessing Explicit Interface Member Implementations

An explicit interface member implementation can be accessed only through a reference to the interface. This means that even other class members can’t directly access them.

For example, the following code shows the declaration of class MyClass, which implements interface IIfc1 with an explicit implementation. Notice that even Method1, which is also a member of MyClass, can’t directly access the explicit implementation.

The first two lines of Method1 produce compile errors because the method is trying to access the implementation directly.

Only the last line in Method1 will compile, because it casts the reference to the current object (this) to a reference to the interface type and uses that reference to the interface to call the explicit interface implementation.

class MyClass : IIfc1

 

{

 

 

 

 

void IIfc1.PrintOut(string s)

// Explicit interface implementation

{

Console.WriteLine("IIfc1");

 

}

 

 

 

 

 

public void Method1()

 

{

PrintOut("...");

// Compile error

 

 

this.PrintOut("...");

// Compile error

 

((IIfc1)this).PrintOut("...");

// OK, call method

}

 

 

 

} Cast to a reference to the interface

This restriction has an important ramification for inheritance. Since other fellow class members can’t directly access explicit interface member implementations, members of classes derived from the class clearly can’t directly access them either. They must always be accessed through a reference to the interface.

430

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]