Книга: C# 2008 Programmer

Abstract Class

Abstract Class

The Shape class does not specify a particular shape, and thus it really does not make sense for you to instantiate it directly, like this:

Shape someShape = new Shape();

Instead, all other shapes should inherit from this base class. To ensure that you cannot instantiate the Shape class directly, you can make it an abstract class by using the abstract keyword:

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);
 }
}

Once a class is defined as abstract, you can no longer instantiate it directly; the following is now not permitted:

//---cannot instantiate directly---
Shape someShape = new Shape();

The abstract keyword indicates that the class is defined solely for the purpose of inheritance; other classes need to inherit from it in order to have objects of this base type.

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


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