Описание:
Переменная AbstractErrorProc определяет процедуру,
которая должна быть вызвана, когда ваш код ошибочно вызывает абстрактный метод
класса
Абстрактный метод является
методом-"заглушкой" - он должен быть реализован только в классах-наследниках.
Родительский класс на самом деле является классом-"скелетом", и таким образом не
следует создавать экземпляры объектов этого класса. Delphi выведет
предупреждение, если вы сделаете так. Если вы вызовете абстрактный метод, он
вызовет AbstractErrorProc перед тем как завершиться с ошибкой. Если
AbstractErrorProc не задана, то он вместо этого сгенерирует исключение
EAbstractError.
Пример кода:
// Full Unit code.
//
-----------------------------------------------------------
// You must
store this code in a unit called Unit1 with a form
// called Form1 that has
an OnCreate event called FormCreate.
unit Unit1;
interface
uses
Forms, Dialogs, Classes, Controls, StdCtrls,
SysUtils;
type
// Define a base TPolygon class :
// This class is a traingle if 3 sides, square if 4 sides ...
TPolygon = class
private
sideCount : Integer; // How many sides?
sideLength : Integer; // How long each side?
shapeArea : Double; // Area of the polygon
protected
procedure setArea; Virtual; Abstract; //
Cannot code until sides known
published
property count : Integer read sideCount;
property length : Integer read sideLength;
property area : Double read shapeArea;
constructor Create(sides, length : Integer);
end;
// Define a square descendent
TSquare =
class(TPolygon)
protected
procedure setArea; override; // Override the abstract method
end;
// Define the form class used by this unit
TForm1 = class(TForm)
procedure
FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm} // Include
form definitions
// Create the TPolygon object
constructor
TPolygon.Create(sides, length : Integer);
begin
// Save the
number and length of the sides
sideCount := sides;
sideLength := length;
// Set the area
using the abstract setArea method :
// This call will be
satisfied only by a subclass
setArea;
end;
//
Implement the abstract setArea parent method for the square
procedure
TSquare.setArea;
begin
// Calculate and save the area of
the square
shapeArea := sideLength * sideLength;
end;
procedure ReportAbstractError;
begin
// Report the
error
ShowMessage('ERROR : Abstract method called.');
end;
// Main line code
procedure TForm1.FormCreate(Sender: TObject);
var
square : TSquare;
polygon : TPolygon;
begin
// Set up a procedure to catch calls to abstract
errors
AbstractErrorProc := Addr(ReportAbstractError);
// Create a square
square :=
TSquare.Create(4, 10);
// Show the area of the square
ShowMessageFmt('Square area = %f',[square.area]);
// Try to create a polygon (internally calls abstract
method)
polygon := TPolygon.Create(2, 10); // Meaningless
shape!
// Show the area of the polygon
ShowMessageFmt('Polygon area = %f',[polygon.area]);
end;
end.
Результат выполнения:
Square area = 100.0
ERROR : Abstract method called.
The program then terminates with an error dialog:
Runtime error 210 at 00452208