Knowing When to Use Override and New Keywords 


Мы поможем в написании ваших работ!



ЗНАЕТЕ ЛИ ВЫ?

Knowing When to Use Override and New Keywords



C# enables methods in derived classes to have the same name as methods in base classes—as long as you are very specific about how the new method should be treated. The following example demonstrates the use of the new and override keywords.

First we declare three classes: a base class called Car, and two classes that derive from it, ConvertibleCar and Minivan. The base class contains a single method, DescribeCar, which sends a description of the car to the console. The derived class methods also include a method called DescribeCar, which displays their unique properties. These methods also call the base class DescribeCar method to demonstrate how they have inherited the properties of the Car class.

In order to highlight the difference, the ConvertibleCar class is defined with the new keyword, while the Minivan class is defined with override.

// Define the base class class Car { public virtual void DescribeCar() { System.Console.WriteLine("Four wheels and an engine."); } } // Define the derived classes class ConvertibleCar: Car { public new virtual void DescribeCar() { base.DescribeCar(); System.Console.WriteLine("A roof that opens up."); } } class Minivan: Car { public override void DescribeCar() { base.DescribeCar(); System.Console.WriteLine("Carries seven people."); } }

 


Использование ключевых слов "Override" и "New"

В C# методы в производных классах могут иметь такие же имена, как и методы в базовых классах (если четко соблюдаются различия в использовании новых методов). В следующем примере демонстрируется использование ключевых слов new и override.

Сначала объявим три класса: базовый класс Car и два класса, производных от него: ConvertibleCar и Minivan. Базовый класс содержит один метод с именем DescribeCar, отправляющий описание автомобиля на консоль. Методы производных классов также включают метод с именем DescribeCar, отображающий уникальные свойства этих классов. Эти методы также вызывают базовый класс DescribeCar, чтобы продемонстрировать, каким образом они унаследовали свойства класса Car.

Чтобы подчеркнуть разницу, класс ConvertibleCar определен с ключевым словом new, а класс Minivan — с ключевым словом override.

ß-----


We can now write some code that declares instances of these classes, and calls their methods so that the objects can describe themselves:

public static void TestCars1() { Car car1 = new Car(); car1.DescribeCar(); System.Console.WriteLine("----------");   ConvertibleCar car2 = new ConvertibleCar(); car2.DescribeCar(); System.Console.WriteLine("----------");   Minivan car3 = new Minivan(); car3.DescribeCar(); System.Console.WriteLine("----------"); }

As you might expect, the output looks like this:

Four wheels and an engine.

----------

Four wheels and an engine.

A roof that opens up.

----------

Four wheels and an engine.

Carries seven people.

----------

 


Теперь можно написать код, описывающий экземпляры этих классов и вызовы методов, чтобы объекты могли себя описывать:

ß---

 

Как и предполагалось, результат выглядит следующим образом:

Four wheels and an engine.

----------

Four wheels and an engine.

A roof that opens up.

----------

Four wheels and an engine.

Carries seven people.

----------

 


However, in this next section of code, we declare an array of objects derived from the Car base class. This array can store Car, ConvertibleCar, and Minivan objects. The array is declared like this:

public static void TestCars2() { Car[] cars = new Car[3]; cars[0] = new Car(); cars[1] = new ConvertibleCar(); cars[2] = new Minivan(); }

We can then use a foreach loop to visit each Car object contained in the array, and call the DescribeCar method, like this:

foreach (Car vehicle in cars) { System.Console.WriteLine("Car object: " + vehicle.GetType()); vehicle.DescribeCar(); System.Console.WriteLine("----------"); }

The output from this loop is as follows:

Car object: YourApplication.Car

Four wheels and an engine.

----------

Car object: YourApplication.ConvertibleCar

Four wheels and an engine.

----------

Car object: YourApplication.Minivan

Four wheels and an engine.

Carries seven people.

----------

Notice how the ConvertibleCar description is not what you might expect. As the new keyword was used to define this method, the derived class method is not called—the base class method is called instead. The Minivan object correctly calls the overridden method, producing the results we expected.

If you want to enforce a rule that all classes derived from Car must implement the DescribeCar method, you should create a new base class that defines the method DescribeCar as abstract. An abstract method does not contain any code, only the method signature. Any classes derived from this base class must provide an implementation of DescribeCar.

 


Однако в следующем фрагменте кода мы объявляем массив объектов, производных от класса Car. В этом массиве могут храниться объекты Car, ConvertibleCar и Minivan. Массив объявляется следующим образом:

public static void TestCars2() { Car[] cars = new Car[3]; cars[0] = new Car(); cars[1] = new ConvertibleCar(); cars[2] = new Minivan(); }

Затем можно использовать цикл foreach для посещения каждого объекта Car в машине и вызова метода DescribeCar:

foreach (Car vehicle in cars) { System.Console.WriteLine("Car object: " + vehicle.GetType()); vehicle.DescribeCar(); System.Console.WriteLine("----------"); }

Результат выполнения этого цикла выглядит следующим образом:

Car object: YourApplication.Car

Four wheels and an engine.

----------

Car object: YourApplication.ConvertibleCar

Four wheels and an engine.

----------

Car object: YourApplication.Minivan

Four wheels and an engine.

Carries seven people.

----------

Обратите внимание, что описание ConvertibleCar не такое, как ожидалось. Поскольку ключевое слово new было использовано для определения этого метода, метод производного класса не вызван, вместо него вызван метод базового класса. Объект Minivan правильно вызывает переопределенный метод и выдает ожидаемый результат.

Чтобы принудительно выполнялось правило, согласно которому все классы, производные от Car, должны реализовать метод DescribeCar, нужно создать новый базовый класс, определяющий метод DescribeCar как abstract. Абстрактный метод содержит только подпись метода и не содержит никакого кода. Любые классы, производные от этого базового класса, должны предоставлять реализацию DescribeCar. Дополнительные сведения см. в разделе abstract.

 



Поделиться:


Последнее изменение этой страницы: 2017-01-19; просмотров: 156; Нарушение авторского права страницы; Мы поможем в написании вашей работы!

infopedia.su Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав. Обратная связь - 18.191.157.186 (0.012 с.)