//---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. 

Sealed Classes and Methods

So far you've seen the class definition for Shape, Rectangle, and Circle. Now let's define a class for the shape Square. As you know, a square is just a special version of rectangle; it just happens to have the same length and width. In this case, the Square class can simply inherit from the Rectangle class:

public class Square : Rectangle {}

You can instantiate the Square class as per normal and all the members available in the Rectangle would then be available to it:

Square s = new Square();

s.length = 5;

s.width = 5;

Console.WriteLine(s.Perimeter()); //---20--- 

Console.WriteLine(s.Area()); //---25--- 

To ensure that no other classes can derive from the Square class, you can seal it using the sealed keyword. A class prefixed with the sealed keyword prevents other classes inheriting from it. For example, if you seal the Square class, like this:

public sealed class Square : Rectangle {}

The following will result in an error:

//---Error: Square is sealed---

public class Rhombus : Square {}

A sealed class cannot contain virtual methods. In the following example, the Square class is sealed, so it cannot contain the virtual method called Diagonal():

public sealed class Square : Rectangle {

 //---Error: sealed class cannot contain virtual methods---

 public virtual Single Diagonal() {

  //---implementation here---

 }

}

This is logical because a sealed class does not provide an opportunity for a derived class to implement its virtual method. By the same argument, a sealed class also cannot contain abstract methods:

public sealed class Square : Rectangle {

 //---Error: sealed class cannot contain abstract method--- 

 public abstract Single Diagonal();

}

You can also seal methods so that other derived classes cannot override the implementation that you have provided in the current class. For example, recall that the Rectangle class provides the implementation for the abstract Area() method defined in the Shape class:

public class Rectangle : Shape {

 public override double Area() {

  return this.length * this.width;

 }

}

To prevent the derived classes of Rectangle (such as Square) from modifying the Area() implementation, prefix the method with the sealed keyword:

public class Rectangle : Shape {

 public override sealed double Area() {

  return this.length * this.width;

 }

}

Now if you try to override the Area() method in the Square class, you get an error:

public sealed class Square : Rectangle {

 //---Error: Area() is sealed in Rectangle class---

 public override double Area() {

  //---implementation here---

 }

}

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату