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

CHAPTER 7 CLASSES AND INHERITANCE

Accessing the Inherited Members

Inherited members are accessed just as if they had been declared in the derived class itself. (Inherited constructors are a bit different—I’ll cover them later in the chapter.) For example, the following code declares classes SomeClass and OtherClass, which were shown in Figure 7-1. The code shows that all four members of OtherClass can be seamlessly accessed, regardless of whether they’re declared in the base class or the derived class.

Main creates an object of derived class OtherClass.

The next two lines in Main call Method1 in the base class, using Field1 from the base class and then Field2 from the derived class.

The subsequent two lines in Main call Method2 in the derived class, again using Field1 from the base class and then Field2 from the derived class.

class SomeClass

// Base class

{

 

 

public string Field1 = "base class field ";

 

public void Method1( string value ) {

 

 

Console.WriteLine("Base class -- Method1:

{0}", value);

}

 

 

}

 

 

class OtherClass: SomeClass

// Derived class

{

 

 

public string Field2 = "derived class field";

 

public void Method2( string value ) { Console.WriteLine("Derived class -- Method2: {0}", value);

}

}

class Program

{

static void Main() {

OtherClass oc = new OtherClass();

 

oc.Method1( oc.Field1 );

//

Base method with base field

oc.Method1( oc.Field2 );

//

Base method with derived field

oc.Method2( oc.Field1 );

//

Derived method with base field

oc.Method2( oc.Field2 );

//

Derived method with derived field

}

 

 

 

}

 

 

 

This code produces the following output:

 

 

 

 

Base class -- Method1:

base class field

 

Base class -- Method1:

derived class field

Derived class -- Method2: base class field

 

Derived class -- Method2:

derived class field

 

 

 

 

163

CHAPTER 7 CLASSES AND INHERITANCE

All Classes Are Derived from Class object

All classes, except special class object, are derived classes, even if they don’t have a class-base specification. Class object is the only class that is not derived, since it is the base of the inheritance hierarchy.

Classes without a class-base specification are implicitly derived directly from class object. Leaving off the class-base specification is just shorthand for specifying that object is the base class. The two forms are semantically equivalent, as shown in Figure 7-2.

Figure 7-2. The class declaration on the left implicitly derives from class object, while the one on the right

explicitly derives from object. The two forms are semantically equivalent.

Other important facts about class derivation are the following:

A class declaration can have only a single class listed in its class-base specification. This is called single inheritance.

Although a class can directly inherit from only a single base class, there is no limit to the level of derivation. That is, the class listed as the base class might be derived from another class, which is derived from another class, and so forth, until you eventually reach object.

Base class and derived class are relative terms. All classes are derived classes, either from object or from another class—so generally when we call a class a derived class, we mean that it is immediately derived from some class other than object. Figure 7-3 shows a simple class hierarchy. After this, I won’t show object in the figures, since all classes are ultimately derived from it.

Figure 7-3. A class hierarchy

164

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