GNU Free Documentation License . .

Object Pascal

: ,
Object Pascal
:

:

:
, , -, [1],

:

:

1986

():

,

:

, (array of const, RTTI, Variant),

:

Delphi (x86 and CLI), Oxygene (CLI), Free Pascal (x86, x86-64, PowerPC, ppc64, SPARC and ARM), Virtual Pascal (x86), TMT Pascal (x86), Turbo51 (Intel 8051)

:

Apple, Turbo Pascal, objfpc, Delphi, Delphi.NET, Oxygene

:

, Smalltalk

:

C#, Java

Object Pascal  , Apple Computer 1986  , . - [2], Clascal, Apple Lisa.

[] Hello World!

[] Object Pascal Apple

program ObjectPascalExample;
 
   type
      THelloWorld = object
         procedure Put;
      end;
 
   var
      HelloWorld: THelloWorld;
 
   procedure THelloWorld.Put;
   begin
      WriteLn('Hello, World!');
   end;
 
begin
   New(HelloWorld);
   HelloWorld.Put;
   Dispose(HelloWorld);
end.

[] Turbo Pascal

Delphi Freepascal . Delphi , Freepascal 

program ObjectPascalExample;
 
   type
      PHelloWorld = ^THelloWorld;
      THelloWorld = object
         procedure Put;
      end;
 
   var
      HelloWorld: PHelloWorld; { this is a pointer to a THelloWorld }
 
   procedure THelloWorld.Put;
   begin
      WriteLn('Hello, World!');
   end;
 
begin
   New(HelloWorld);
   HelloWorld^.Put;
   Dispose(HelloWorld);
end.

[] Object Pascal Delphi Free Pascal

Free Pascal ObjFpc Delphi[3]

program ObjectPascalExample;
 
type
  THelloWorld = class                    {   }
    procedure Put;
  end;
 
procedure THelloWorld.Put;               {    Put  THelloWorld }
begin
  Writeln('Hello, World!');
end;
 
var
  HelloWorld: THelloWorld;               {  -    }
 
begin
  HelloWorld := THelloWorld.Create;      {        }
  HelloWorld.Put;
  HelloWorld.Free;                       {         }
end.

[] Object Pascal Borland Turbo Pascal

, , :

  • (fundamental) . ( ) Object Pascal .
  • (generic) . , .

[]

. : Delphi

[] ( )

, . ( overload)  :
procedure Calc(I: Integer); overload;
  
procedure Calc(S: String; J: Integer); overload;

[]

heap- (), , . . .
:

var MyFlexibleArray: array of Real;
:

var
A, B: array of Integer; begin SetLength(A, 1); { } A[0] := 1; B := A; B[0] := 2; end;

, , :
type TDynamicCharArray = array of Char;
function Find(A: TDynamicCharArray): Integer;

[]

[]

Object Pascal Borland , as is . (variant open array parameters).

[]

Object Pascal (Variant), . Variant . , , .

(, , , Currency, OLE-), , COM CORBA , . Variant :

  • ;
  • ;
  • Int64 ( Delphi 6  ).

Variant ( ) , , . . , , , , (V[i] ).

var
  V1, V2, V3, V4, V5: Variant;
  I: Integer;
  D: Double;
  S: string;
begin
  V1 := 1; {   integer }
  V2 := 359.768; {   real }
  V3 := 'Hello world!'; {   string }
...

[]

. «variant open array parameters». . High . array of const. :

function Output(const Args: array of const): string;
var
 I: Integer;
begin
 Result := ;
 for I := 0 to High(Args) do   with Args[I] do
   case VType of
       vtString:     Result := Result + VString^;
       vtPChar:      Result := Result + VPChar;
       vtInteger:    Result := Result + IntToStr(VInteger);
       vtBoolean:    Result := Result + BoolToStr(VBoolean);
       vtChar:       Result := Result + VChar;
       vtExtended:   Result := Result + FloatToStr(VExtended^);
       vtObject:     Result := Result + VObject.ClassName;
       vtClass:      Result := Result + VClass.ClassName;
       vtVariant:    Result := Result + string(VVariant^);
       vtInt64:      Result := Result + IntToStr(VInt64^);
       vtAnsiString: Result := Result + string(VAnsiString);
       vtCurrency:   Result := Result + CurrToStr(VCurrency^);
   end;
  Result:=Result+ ' ';
end;
...
Output(['test', 777, '@', True, 3.14159, TForm]) {   }

: «test 777 @ T 3.14159 TForm».

, , . , , .

[]

class ( Turbo Pascal object).

is as . , object:
type
   TMyMethod = procedure (Sender : Object) of object;

[] , -

Turbo Pascal , . Object Pascal , heap- (). . , New, « » (^). .
:

Turbo Pascal ( , )
type
 PMyObject = ^TMyObject;
 TMyObject = object (TObject)
 MyField : PMyType;
 constructor Init;
end;
...
var
MyObject : PMyObject;
begin
 MyObject:=New(PMyObject,Init);
 MyObject^.MyField:= ...
end;
Object Pascal
type
TMyObject = class (TObject)
  MyField : TMyType;
  constructor Create;
end;
...
var
MyObject : TMyObject;
begin
  MyObject:=TMyObject.Create;
  MyObject.MyField:= ...
end;

. , . New , . Create.

(, ), . . protected () .

[] -

(property) read, write, stored, default (nodefault), index. , published , .

[]

type
 generic TList<T> = class     {}
   Items: array of T;
   procedure Add(Value: T);
 end;
Implementation
{}
procedure TList.Add(Value: T);
begin
  SetLength(Items, Length(Items) + 1);
  Items[Length(Items) - 1] := Value;
end;

specialize:

Type
  TIntegerList = specialize TList<Integer>;
  TPointerList = specialize TList<Pointer>;
  TStringList  = specialize TList<string>;

[]

TMT Pascal ( Object Pascal) , : Delphi ( Delphi 2005), Free Pascal .
:

{}
type
  TVector = packed record
     A, B, C: Double;
     procedure From(const A, B, C: Double);
     class operator Add(const Left, Right: TVector): TVector;
     class operator Implicit(const v: TVector): TPoint;
  end;
 implementation
...
{}
class operator TVector.Add(const Left, Right: TVector): TVector;
begin
 Result.A := Left.A + Right.A;
 Result.B := Left.B + Right.B;
 Result.C := Left.C + Right.C;
end;
class operator TVector.Implicit(const v: TVector): TPoint; begin Result.A := round(v.A); Result.B := round(v.B); end;
... {:} var v1, v2: TVector; begin v1.From(20, 70, 0); v2.From(15, 40, 4); Canvas.Polygon([v1, v2, v1 + v2]); end;

[]

Delphi 7, Borland Object Pascal Delphi[4].

Object Pascal . Object Pascal ( Delphi)  TopSpeed Pascal ( Turbo Pascal[5]) TopSpeed, TMT Pascal, Virtual Pascal, PascalABC.NET, Free Pascal, GNU Pascal. Object Pascal Oxygene.

[]

  1. Hallvards Blog: Highlander2 Beta: Generics in Delphi for .NET
  2. . - [itbookz.ifolder.ru/7276278]
  3. Michaël Van Canneyt Chapter 6: Classes  (.). Free Pascal : Reference guide. ( 2011). 2 2012. 16 2012.
  4. Delphi Language Overview
  5. TopSpeed-: