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

CHAPTER 17 INTERFACES

Interfaces Can Inherit Interfaces

You saw earlier that interface implementations can be inherited from base classes. But an interface itself can inherit from one or more other interfaces.

To specify that an interface inherits from other interfaces, place the names of the base interfaces in a comma-separated list after a colon following the interface name in the interface declaration,

as shown here:

 

Colon

Base interface list

interface IDataIO : IDataRetrieve, IDataStore

{...

Unlike a class, which can have only a single class name in its base class list, an interface can have any number of interfaces in its base interface list.

The interfaces in the list can themselves have inherited interfaces.

The resulting interface contains all the members it declares, as well as all those of its base

interfaces.

The code in Figure 17-10 shows the declaration of three interfaces. Interface IDataIO inherits from the first two. The figure on the right shows IDataIO encompassing the other two interfaces.

Figure 17-10. Class with interface inheriting multiple interfaces

431

CHAPTER 17 INTERFACES

Example of Different Classes Implementing an Interface

The following code illustrates several aspects of interfaces that have been covered. The program declares a class called Animal, which is used as a base class for several other classes that represent various types of animals. It also declares an interface named ILiveBirth.

Classes Cat, Dog, and Bird all derive from base class Animal. Cat and Dog both implement the ILiveBirth interface, but class Bird does not.

In Main, the program creates an array of Animal objects and populates it with a class object of each of the three types of animal classes. The program then iterates through the array and, using the as operator, retrieves references to the ILiveBirth interface of each object that has one and calls its

BabyCalled method.

interface ILiveBirth

// Declare interface

{

 

string BabyCalled();

 

}

 

class Animal { }

// Base class Animal

class Cat : Animal, ILiveBirth

// Declare class Cat

{

 

string ILiveBirth.BabyCalled()

 

{ return "kitten"; }

 

}

 

class Dog : Animal, ILiveBirth

// Declare class Dog

{

 

string ILiveBirth.BabyCalled()

 

{ return "puppy"; }

 

}

 

class Bird : Animal

// Declare class Bird

{

 

}

 

432

CHAPTER 17 INTERFACES

class Program

 

 

{

 

 

static void Main()

 

 

{

 

 

Animal[] animalArray

= new Animal[3];

// Create Animal array

animalArray[0] = new

Cat();

// Insert Cat class object

animalArray[1] = new

Bird();

// Insert Bird class object

animalArray[2] = new

Dog();

// Insert Dog class object

foreach( Animal a in

animalArray )

// Cycle through array

{

 

 

ILiveBirth b = a as ILiveBirth;

// if implements ILiveBirth...

if (b != null)

Console.WriteLine("Baby is called: {0}", b.BabyCalled());

}

}

}

This code produces the following output:

Baby is called: kitten

Baby is called: puppy

Figure 17-11 illustrates the array and the objects in memory.

Figure 17-11. Different object types of base class Animal are interspersed in the array.

433

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