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

CHAPTER 17 INTERFACES

Implementing Interfaces with Duplicate Members

Since a class can implement any number of interfaces, it’s possible that two or more of the interface members might have the same signature and return type. So, how does the compiler handle that situation?

For example, suppose you had two interfaces—IIfc1 and IIfc2—as shown following. Each interface has a method named PrintOut, with the same signature and return type. If you were to create a class that implemented both interfaces, how should you handle these duplicate interface methods?

interface IIfc1

{

void PrintOut(string s);

}

interface IIfc2

{

void PrintOut(string t);

}

The answer is that if a class implements multiple interfaces, where several of the interfaces have members with the same signature and return type, the class can implement a single member that satisfies all the interfaces containing that duplicated member.

For example, the following code shows the declaration of class MyClass, which implements both IIfc1 and IIfc2. Its implementation of method PrintOut satisfies the requirement for both interfaces.

class MyClass : IIfc1, IIfc2

//

Implement both interfaces.

{

 

 

public void PrintOut(string s)

//

Single implementation for both

{

 

 

Console.WriteLine("Calling through: {0}", s);

}

}

class Program

{

static void Main()

{

MyClass mc = new MyClass(); mc.PrintOut("object");

}

}

422

CHAPTER 17 INTERFACES

This code produces the following output:

Calling through: object

Figure 17-5 illustrates the duplicate interface methods being implemented by a single class-level method implementation.

Figure 17-5. Multiple interfaces implemented by the same class member

423

CHAPTER 17 INTERFACES

References to Multiple Interfaces

You saw previously that interfaces are reference types and that you can get a reference to an interface by using the as operator or 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

// Declare interface

{

 

void PrintOut(string s);

 

}

 

interface IIfc2

// Declare interface

{

 

void PrintOut(string s);

 

}

 

class MyClass : IIfc1, IIfc2

// Declare class

{

 

public void PrintOut(string s)

 

{

 

Console.WriteLine("Calling through:

{0}", s);

}

 

}

 

 

 

424

CHAPTER 17 INTERFACES

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. Separate references to different interfaces in the class

425

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