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

CHAPTER 7 CLASSES AND INHERITANCE

Overriding a Method Marked override

Overriding methods can occur between any levels of inheritance.

When you use a reference to the base class part of an object to call an overridden method, the method call is passed up the derivation hierarchy for execution to the most-derived version of the method marked as override.

If there are other declarations of the method at higher levels of derivation that are not marked as override—they are not invoked.

For example, the following code shows three classes that form an inheritance hierarchy: MyBaseClass, MyDerivedClass, and SecondDerived. All three classes contain a method named Print, with the same signature. In MyBaseClass, Print is labeled virtual. In MyDerivedClass, it’s labeled override. In class SecondDerived, you can declare method Print with either override or new. Let’s look at what happens in each case.

class MyBaseClass

// Base class

{

 

virtual public void Print()

 

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

 

}

 

class MyDerivedClass : MyBaseClass

// Derived class

{

 

override public void Print()

 

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

}

 

class SecondDerived : MyDerivedClass

// Most-derived class

{

 

... // Given in the following pages

 

}

 

Case 1: Declaring Print with override

If you declare the Print method of SecondDerived as override, then it will override both the less-derived versions of the method, as shown in Figure 7-9. If a reference to the base class is used to call Print, it gets passed all the way up the chain to the implementation in class SecondDerived.

172

CHAPTER 7 CLASSES AND INHERITANCE

The following code implements this case. Notice the code in the last two lines of method Main.

The first of the two statements calls the Print method by using a reference to the most-derived class—SecondDerived. This is not calling through a reference to the base class portion, so it will call the method implemented in SecondDerived.

The second statement, however, calls the Print method by using a reference to the base class—

MyBaseClass.

class SecondDerived : MyDerivedClass

 

{

 

 

 

 

override public void Print() {

 

 

 

 

 

 

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

}

 

 

 

}

 

 

 

 

class Program

 

{

 

 

 

 

static void Main()

 

{

 

 

 

 

SecondDerived derived = new SecondDerived();

// Use SecondDerived.

 

MyBaseClass mybc = (MyBaseClass)derived;

// Use MyBaseClass.

derived.Print();

mybc.Print();

}

}

The result is that regardless of whether Print is called through the derived class or the base class, the method in the most-derived class is called. When called through the base class, it’s passed up the inheritance hierarchy. This code produces the following output:

This is the second derived class.

This is the second derived class.

Figure 7-9. Execution is passed to the top of the chain of multiple levels of override.

173

CHAPTER 7 CLASSES AND INHERITANCE

Case 2: Declaring Print with new

If instead you declare the Print method of SecondDerived as new, the result is as shown in Figure 7-10. Main is the same as in the previous case.

class SecondDerived : MyDerivedClass

{

new public void Print()

{

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

}

}

 

class Program

 

{

 

static void Main()

// Main

{

 

SecondDerived derived = new SecondDerived();

// Use SecondDerived.

MyBaseClass mybc = (MyBaseClass)derived;

// Use MyBaseClass.

derived.Print();

 

mybc.Print();

 

}

 

}

 

The result is that when method Print is called through the reference to SecondDerived, the method in SecondDerived is executed, as you would expect. When the method is called through a reference to MyBaseClass, however, the method call is passed up only one level, to class MyDerived, where it is executed. The only difference between the two cases is whether the method in SecondDerived is declared with modifier override or modifier new.

This code produces the following output:

This is the second derived class.

This is the derived class.

Figure 7-10. Hiding the overridden methods

174

CHAPTER 7 CLASSES AND INHERITANCE

Overriding Other Member Types

In the previous few sections, you’ve seen how the virtual/override designations work on methods. These work exactly the same way with properties, events, and indexers. For example, the following code shows a read-only property named MyProperty using virtual/override.

class MyBaseClass

{

private int _myInt = 5; virtual public int MyProperty

{

get { return _myInt; }

}

}

class MyDerivedClass : MyBaseClass

{

private int _myInt = 10; override public int MyProperty

{

get { return _myInt; }

}

}

 

class Program

 

{

 

static void Main()

 

{

 

MyDerivedClass derived

= new MyDerivedClass();

MyBaseClass mybc

= (MyBaseClass)derived;

Console.WriteLine( derived.MyProperty ); Console.WriteLine( mybc.MyProperty );

}

}

This code produces the following output:

10

10

175

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