Книга: C# 2008 Programmer

Virtual Methods

Virtual Methods

Using the Rectangle class, you can find the perimeter and area of a rectangle with the Perimeter() and Area() methods, respectively. But what if you want to define a Circle class? Obviously, the perimeter (circumference) of a circle is not the length multiply by its width. For simplicity, though, let's assume that the diameter of a circle can be represented by the Length property.

The definition of Circle will look like this:

public class Circle : Shape {}

However, the Perimeter() method should be reimplemented as the circumference of a circle is defined to be 2*?*radius (or ?*diameter). But the Perimeter() method has already been defined in the base class Shape. In this case, you need to indicate to the compiler that the Perimeter() method in the Shape class can be reimplemented by its derived class. To do so, you need to prefix the Perimeter() method with the virtual keyword to indicate that all derived classes have the option to change its implementation:

public abstract class Shape {
 //---properties---
 public double length { get; set; }
 public double width { get; set; }
 //---make this method as virtual---
 public virtual double Perimeter() {
  return 2 * (this.length + this.width);
 }
 //---abstract method--- 
 public abstract double Area();
}

The Circle class now has to provide implementation for both the Perimeter() and Area() methods (note the use of the override keyword):

public class Circle : Shape {
 //---provide the implementation for the abstract method---
 public override double Perimeter() {
  return Math.PI * (this.length);
 }
 //---provide the implementation for the virtual method---
 public override double Area() {
  return Math.PI * Math.Pow(this.length/2, 2);
 }
}

Bear in mind that when overriding a method in the base class, the new method must have the same signature (parameter) as the overridden method. For example, the following is not allowed because the new Perimeter() method has a single input parameter, but this signature does not match that of the Perimeter() method defined in the base class (Shape):

public class Circle : Shape {
 //---signature does not match Perimeter() in base class---
 public override double Perimeter(int Diameter) {
   //...
 }
}

If you need to implement another new method also called Perimeter() in the Circle class but with a different signature, use the new keyword, like this:

public class Circle : Shape {
 //---a new Perimeter() method---
 public new double Perimeter(int diameter) {
  //...
 }
}

When a class has multiple methods each with the same name but a different signature (parameter), the methods are known as overloaded. The Perimeter() method of the Circle class is now overloaded (see Figure 6-2). Note that IntelliSense shows that the first method is from the Shape base class, while the second one is from the Circle class.


Figure 6-2 

See the "Overloading Methods" section later in this chapter. 

Оглавление книги


Генерация: 0.932. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз