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

Chapter 11 Understanding Parameter Arrays

215

If you want to continue to the next chapter:

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

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 11 Quick Reference

To

Write a method that accepts any number of arguments of a given type

Do this

Write a method whose parameter is a params array of the given type. For example, a method that accepts any number of bool arguments would be declared like this:

someType Method(params bool[] flags)

{

...

}

Write a method that accepts any number of arguments of any type

Write a method whose parameter is a params array whose elements are of type object. For example:

someType Method(params object[] paramList)

{

...

}

Chapter 12

Working with Inheritance

After completing this chapter, you will be able to:

Create a derived class that inherits features from a base class.

Control method hiding and overriding by using the new, virtual, and override keywords.

Limit accessibility within an inheritance hierarchy by using the protected keyword.

Define extension methods as an alternative mechanism to using inheritance.

Inheritance is a key concept in the world of object orientation. You can use inheritance as a tool to avoid repetition when defining different classes that have a number of features in

common and are quite clearly related to each other. Perhaps they are different classes of the same type, each with its own distinguishing feature—for example, managers, manual workers, and all employees of a factory. If you were writing an application to simulate the fac-

tory, how would you specify that managers and manual workers have a number of features that are the same but also have other features that are different? For example, they all have an employee reference number, but managers have different responsibilities and perform different tasks than manual workers.

This is where inheritance proves useful.

What Is Inheritance?

If you ask several experienced programmers what they understand by the term inheritance,

you will typically get different and conflicting answers. Part of the confusion stems from the fact that the word inheritance itself has several subtly different meanings. If someone be-

queaths something to you in a will, you are said to inherit it. Similarly, we say that you inherit

half of your genes from your mother and half of your genes from your father. Both of these uses of the word inheritance have very little to do with inheritance in programming.

Inheritance in programming is all about classification—it’s a relationship between classes. For example, when you were at school, you probably learned about mammals, and you learned that horses and whales are examples of mammals. Each has every attribute that a mammal does (it breathes air, it suckles its young, it is warm-blooded, and so on), but each also has its own special features (a horse has hooves, unlike a whale).

How could you model a horse and a whale in a program? One way would be to create two distinct classes named Horse and Whale. Each class could implement the methods that are

217

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