Книга: C# 2008 Programmer

Abstract Methods

Abstract Methods

Besides making a class abstract by using the abstract keyword, you can also create abstract methods. An abstract method has no implementation, and its implementation is left to the classes that inherit from the class that defines it. Using the Shape class as an example, you can now define an abstract method called Area() that calculates the area of a shape:

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

It is logical to make the Area() method an abstract one because at this point you don't really know what shape you are working on (circle, square, or triangle, for example), and thus you don't know how to calculate its area.

An abstract method is defined just like a normal method without the normal method block ({}). Classes that inherit from a class containing abstract methods must provide the implementation for those methods.

The Rectangle class defined earlier must now implement the Area() abstract method, using the override keyword:

public class Rectangle : Shape {
 //---provide the implementation for the abstract method---
 public override double Area() {
  return this.length * this.width;
 }
}

Instead of using the this keyword to access the length and width properties, you can also use the base keyword:

public class Rectangle : Shape {
 public override double Area() {
  return base.length * base.width;
 }
}

The base keyword is used to access members (such as properties and variables) of the base class from within a derived class. You can also use the base keyword to access methods from the base class; here's an example:

public class Rectangle : Shape {
 public override sealed double Area() {
  return this.length * this.width;
  //return base.length * base.width;
 }
 public override double Perimeter() {
  //---invokes the Perimeter() method in the Shape class---
  return base.Perimeter();
 }
}

You can now use the Rectangle class like this:

Rectangle r = new Rectangle();
r.length = 4;
r.width = 5;
Console.WriteLine(r.Perimeter()); //---18---
Console.WriteLine(r.Area()); //---20---

An abstract method can only be defined in an abstract class.

The base keyword refers to the parent class of a derived class, not the root class. Consider the following example where you have three classes — Class3 inherits from Class2, which in turn inherits from Class1:

public class Class1 {
 public virtual void PrintString() {
  Console.WriteLine("Class1");
 }
}
public class Class2: Class1 {
 public override void PrintString() {
  Console.WriteLine("Class2");
 }
}
public class Class3 : Class2 {
 public override void PrintString() {
  base.PrintString();
 }
}

In Class3, the base.PrintString() statement invokes the PrintString() method defined in its parent, Class2. The following statements verify this:

Class3 c3 = new Class3();
//---prints out "Class2"---
c3.PrintString();

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


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