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

CHAPTER 7 CLASSES AND INHERITANCE

Abstract Members

An abstract member is a function member that is designed to be overridden. An abstract member has the following characteristics:

It is marked with the abstract modifier.

It doesn’t have an implementation code block. The code blocks of abstract members are represented by semicolons.

For example, the following code from inside a class definition declares two abstract members: an abstract method called PrintStuff and an abstract property called MyProperty. Notice the semicolons in place of the implementation blocks.

Keyword

Semicolon in place of implementation

abstract public void PrintStuff(string s);

abstract public int MyProperty

{

get; ← Semicolon in place of implementation set; ← Semicolon in place of implementation

}

Abstract members can be declared only in abstract classes, which we’ll look at in the next section. Four type of member can be declared as abstract:

Methods

Properties

Events

Indexers

190

CHAPTER 7 CLASSES AND INHERITANCE

Other important facts about abstract members are the following:

Abstract members, although they must be overridden by a corresponding member in a derived class, cannot use the virtual modifier in addition to the abstract modifier.

As with virtual members, the implementation of an abstract member in a derived class must specify the override modifier.

Table 7-3 compares and contrasts virtual members and abstract members.

Table 7-3. Comparing Virtual and Abstract Members

 

Virtual Member

Abstract Member

Keyword

virtual

abstract

Implementation body

Has an implementation body

No implementation body—

 

 

semicolon instead

Overridden in a derived class

Can be overridden—

Must be overridden—

 

using override

using override

Types of members

Methods

Methods

 

Properties

Properties

 

Events

Events

 

Indexers

Indexers

 

 

 

191

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