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

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

// Declare base class.

{

 

public void PrintOut(string s)

// Declare the method.

{

 

Console.WriteLine("Calling through:

{0}", s);

}

 

}

 

class Derived : MyBaseClass, IIfc1

// Declare class.

{

 

}

 

class Program {

 

static void Main()

 

{

 

Derived d = new Derived();

// Create class object

d.PrintOut("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

426

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