Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
27
Добавлен:
25.03.2016
Размер:
14 Мб
Скачать
☆

244 Part II Understanding the C# Language

Working with Multiple Interfaces

A class can have at most one base class, but it is allowed to implement an unlimited number of interfaces. A class must still implement all the methods it inherits from all its interfaces.

If an interface, a structure, or a class inherits from more than one interface, you write the

interfaces in a comma-separated list. If a class also has a base class, the interfaces are listed after the base class. For example, suppose you define another interface named IGrazable that contains the ChewGrass method for all grazing animals. You can define the Horse class like

this:

class Horse : Mammal, ILandBound, IGrazable

{

...

}

Abstract Classes

The ILandBound and IGrazable interfaces could be implemented by many different classes, depending on how many different types of mammals you want to model in your C# application. In situations such as this, it’s quite common for parts of the derived classes to share common implementations. For example, the duplication in the following two classes is obvious:

class Horse : Mammal, ILandBound, IGrazable

{

...

void IGrazable.ChewGrass()

{

Console.WriteLine(“Chewing grass”); // code for chewing grass

};

}

class Sheep : Mammal, ILandBound, IGrazable

{

...

void IGrazable.ChewGrass()

{

Console.WriteLine(“Chewing grass”);

// same code as horse for chewing grass

};

}

Duplication in code is a warning sign. You should refactor the code to avoid the duplication and reduce any maintenance costs. The way to achieve this refactoring is to put the common

Chapter 13 Creating Interfaces and Defining Abstract Classes

245

implementation into a new class created specifically for this purpose. In effect, you can insert a new class into the class hierarchy. For example:

class GrazingMammal : Mammal, IGrazable

{

...

void IGrazable.ChewGrass()

{

Console.WriteLine(“Chewing grass”); // common code for chewing grass

}

}

class Horse : GrazingMammal, ILandBound

{

...

}

class Sheep : GrazingMammal, ILandBound

{

...

}

This is a good solution, but there is one thing that is still not quite right: You can actually create instances of the GrazingMammal class (and the Mammal class for that matter). This doesn’t really make sense. The GrazingMammal class exists to provide a common default implementation. Its sole purpose is to be inherited from. The GrazingMammal class is an

abstraction of common functionality rather than an entity in its own right.

To declare that creating instances of a class is not allowed, you must explicitly declare that the class is abstract, by using the abstract keyword. For example:

abstract class GrazingMammal : Mammal, IGrazable

{

...

}

If you try to instantiate a GrazingMammal object, the code will not compile:

GrazingMammal myGrazingMammal = new GrazingMammal(...); // illegal

Abstract Methods

An abstract class can contain abstract methods. An abstract method is similar in principle to a virtual method (you met virtual methods in Chapter 12) except that it does not contain a

method body. A derived class must override this method. The following example defines the DigestGrass method in the GrazingMammal class as an abstract method; grazing mammals

might use the same code for chewing grass, but they must provide their own implementation of the DigestGrass method. An abstract method is useful if it does not make sense to provide

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