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

CHAPTER 7 CLASSES AND INHERITANCE

Using References to a Base Class

An instance of a derived class consists of an instance of the base class, plus the additional members of the derived class. A reference to the derived class points to the whole class object, including the base class part.

If you have a reference to a derived class object, you can get a reference to just the base class part of the object by casting the reference to the type of the base class by using the cast operator. The cast operator is placed in front of the object reference and consists of a set of parentheses containing the name of the class being cast to. Casting is covered in detail in Chapter 18.

The next few sections cover accessing an object by using a reference to the base class part of the object. We’ll start by looking at the two lines of code that follow, which declare references to objects. Figure 7-6 illustrates the code and shows the parts of the object seen by the different variables.

The first line declares and initializes variable derived, which then contains a reference to an object of type MyDerivedClass.

The second line declares a variable of the base class type, MyBaseClass, and casts the reference in derived to that type, giving a reference to the base class part of the object.

The reference to the base class part is stored in variable mybc, on the left side of the assignment operator.

The reference to the base class part cannot “see” the rest of the derived class object, because it’s “looking” at it through a reference to the base type.

MyDerivedClass derived

=

new MyDerivedClass();

//

Create an object.

MyBaseClass mybc

=

(MyBaseClass) derived;

//

Cast the reference.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 7-6. Reference derived can see the entire MyDerivedClass object, while mybc can only see the

MyBaseClass part of the object.

168

CHAPTER 7 CLASSES AND INHERITANCE

The following code shows the declaration and use of these two classes. Figure 7-7 illustrates the object and references in memory.

Main creates an object of type MyDerivedClass and stores its reference in variable derived. Main also creates a variable of type MyBaseClass and uses it to store a reference to the base class portion of the object. When the Print method is called on each reference, the call invokes the implementation of the method that the reference can see, producing different output strings.

class MyBaseClass

{

public void Print()

{

Console.WriteLine("This is the base class.");

}

}

class MyDerivedClass : MyBaseClass

{

new public void Print()

{

Console.WriteLine("This is the derived class.");

}

}

class Program

{

static void Main()

{

MyDerivedClass derived = new MyDerivedClass();

MyBaseClass mybc = (MyBaseClass)derived;

 

 

 

 

Cast to base class

derived.Print();

 

// Call Print from derived portion.

mybc.Print();

 

// Call Print from base portion.

}

}

This code produces the following output:

This is the derived class.

This is the base class.

Figure 7-7. A reference to the derived class and the base class

169

CHAPTER 7 CLASSES AND INHERITANCE

Virtual and Override Methods

In the previous section, you saw that when you access an object of a derived class by using a reference to the base class, you get the members from the base class. Virtual methods allow a reference to the base class to access “up into” the derived class.

You can use a reference to a base class to call a method in the derived class, if the following are true:

The method in the derived class and the method in the base class each have the same signature and return type.

The method in the base class is labeled virtual.

The method in the derived class is labeled override.

For example, the following code shows the virtual and override modifiers on the methods in the base class and derived class:

class MyBaseClass

// Base class

{

 

 

 

 

virtual public void Print()

 

 

 

 

...

 

 

 

class MyDerivedClass : MyBaseClass

// Derived class

{

 

 

 

 

override public void Print()

 

 

 

 

Figure 7-8 illustrates this set of virtual and override methods. Notice how the behavior differs from the previous case, where I used new to hide the base class members.

When the Print method is called by using the reference to the base class (mybc), the method call is passed up to the derived class and executed, because

The method in the base class is marked as virtual.

There is a matching override method in the derived class.

Figure 7-8 illustrates this by showing the arrow coming out the back of the virtual Print method and pointing at the override Print method.

Figure 7-8. A virtual method and an override method

170

derived.Print();
mybc.Print();

CHAPTER 7 CLASSES AND INHERITANCE

The following code is the same as in the previous section, but this time, the methods are labeled virtual and override. This produces a result that is very different from that of the previous example. In this version, calling the method through the base class invokes the method in the derived class.

class MyBaseClass

{

virtual public void Print()

{

Console.WriteLine("This is the base class.");

}

}

class MyDerivedClass : MyBaseClass

{

override public void Print()

{

Console.WriteLine("This is the derived class.");

}

}

class Program

{

static void Main()

{

MyDerivedClass derived = new MyDerivedClass(); MyBaseClass mybc = (MyBaseClass)derived;

Cast to base class

}

}

This code produces the following output:

This is the derived class.

This is the derived class.

Other important things to know about the virtual and override modifiers are the following:

The overriding and overridden methods must have the same accessibility. In other words, the overridden method cannot be, for example, private, and the overriding method public.

You cannot override a method that is static or is nonvirtual.

Methods, properties, and indexers (which I covered in the preceding chapter), and another member type, called events (which I’ll cover later in the text), can all be declared virtual and override.

171

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