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

Chapter 13 Creating Interfaces and Defining Abstract Classes

255

Summarizing Keyword Combinations

The following table summarizes the various valid (yes), invalid (no), and mandatory (required) keyword combinations when creating classes and interfaces.

Keyword

Interface

Abstract class

Class

Sealed class

Structure

abstract

no

yes

no

no

no

 

 

 

 

 

 

new

yes1

yes

yes

yes

no2

override

no

yes

yes

yes

no3

private

no

yes

yes

yes

yes

 

 

 

 

 

 

protected

no

yes

yes

yes

no4

public

no

yes

yes

yes

yes

 

 

 

 

 

 

sealed

no

yes

yes

required

no

virtual

no

yes

yes

no

no

 

 

1

An interface can extend another interface and introduce a new method with the same signature.

2

A structure implicitly derives from System.Object, which contains methods that the structure can hide.

3

A structure implicitly derives from System.Object, which contains no virtual methods.

 

4 A structure is implicitly sealed and cannot be derived from.

256

Part II Understanding the C# Language

If you want to continue to the next chapter:

Keep Visual Studio 2008 running, and turn to Chapter 14.

If you want to exit Visual Studio 2008 now:

On the File menu, click Exit. If you see a Save dialog box, click Yes (if you are using Visual Studio 2008) or Save (if you are using Visual C# 2008 Express Edition) and save the project.

Chapter 13 Quick Reference

To

Do this

Declare an interface

Use the interface keyword. For example:

 

interface IDemo

 

{

 

string Name();

 

string Description();

 

}

Implement an interface

Declare a class using the same syntax as class inheritance, and then

 

implement all the member functions of the interface. For example:

 

class Test : IDemo

 

{

 

public string IDemo.Name()

 

{

 

...

 

}

 

public string IDemo.Description()

 

{

 

...

 

}

 

}

Create an abstract class that can be used only as a base class, containing abstract methods

Declare the class using the abstract keyword. For each abstract method, declare the method with the abstract keyword and without a method

body. For example:

abstract class GrazingMammal

{

abstract void DigestGrass();

...

}

Create a sealed class that cannot be used as a base class

Declare the class using the sealed keyword. For example:

sealed class Horse

{

...

}

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