.


:




:

































 

 

 

 


3. Object Pascal




 

Object Pascal

ᒺ, ᒺ . . .

type < > = class (< >)

private

{, }

protected

{, , }

public

{, , }

published

{, , }

end;

 

- ; , . , , TObject. ObjectPascal TObject ᒺ.

.

ᒺ ( ).

ᒺ ( ).

.

Private (, ) , ᒺ .

Protected () , .

Public () , , , ᒺ .

Published ( ) , , , ᒺ Delphi ( ).

, , Published.

 

: TPoint

 

type

TPoint = class

X, Y: Real; //

function Ro: Real; //

function Fi: Real; //

function Distance (p: TPoint): Real;

// ;

procedure Translate (dx, dy: Real);

// ;

procedure Rotate (p:TPoint; f: Real);

// ( );

procedure Scale (k: Real);

//

end;

 

:

X, Y ;

Ro, Fi, Distance, Translate, Rotate, Scale .

 

, X Y , Ro Fi :

//

Ro, Fi: Real; //

function X: Real; //

function Y: Real; //

 

Object Pascal , (. interface). , (. implementation).

 

implementation

 

function TPoint.Ro: Real;

begin

Ro:=sqrt(sqr(X)+sqr(Y));

//

// Result:=sqrt(sqr(X)+sqr(Y));

end;

 

function TPoint.Fi: Real; //

begin

Fi:= ArcTan(Y/X);

end;

 

function TPoint.Distance (p: TPoint): Real;

begin

Distance:=sqrt(sqr(X-p.X)+sqr(Y-p.Y));

end;

 

procedure TPoint.Translate (dx, dy: Real);

begin

X:=X+dx;

Y:=Y+dy;

end;

 

procedure TPoint.Rotate (p:TPoint; f: Real);

var x1, y1: real;

begin

x1:=(X-p.X)*cos(f)-(Y-p.Y)*sin(f)+p.X;

y1:=(X-p.X)*sin(f)+(Y-p.Y)*cos(f)+p.Y;

X:=x1; Y:=y1;

end;

 

procedure TPoint.Scale (k: Real);

begin

X:=k*X;

Y:=k*Y;

end;

ᒺ Object Pascal. ᒺ

. ᒺ , ᒺ.

 

 

var P1, P2: TPoint;

 

ᒺ ᒺ. -ᒺ (4- ). nil ( ).

, ᒺ Object Pascal , . , ᒺ, , (^), New Dispose .

, constructor. , Create.

:

 

type TPoint= class

//

constructor Create;

//

end;

 

var P1, P2: TPoint;

 

constructor TPoint.Create;

Begin

X:=0;

Y:=0;

End;

 

begin

P1:= TPoint.Create;

P2:= TPoint.Create;

//

end;

 

.

 

:

 

constructor TPoint.Create (a, b: Real);

begin

X:=a;

Y:=b;

end;

 

, :

 

P1:=TPoint.Create (1, 2);

 

Self, ᒺ, . , () , :

 

Self.X:= X;

Self.Y:= Y;

 

, ᒺ, . 0, - (#0), false, nil.

, new ᒺ , . , ᒺ .

ᒺ, .

 

( ) () . :

 

X1:=P1.X; // ;

Ro1:=P1.Ro; // .

 

ᒺ, , (ᒺ 1 Ro);

 

ᒺ , ,

 

P1.X:=6; //

 

. , ᒺ D 1, D 2 TDate ( ) Year, Month, Day:

 

D1.Month:= 13; D2.Day:= 32; // !

, , ( ). (. property):

property < >: <>

read < >

write < >

default < >;

 

, ( GetMonth, GetDay). .

, (SetMonth, SetDay). . .

, .

, ᒺ ( Month Day 1).

:

 

type TDate = class

private

aDay, aMonth, aYear: integer

protected

procedure SetDay (value: integer);

//

published

property Day: integer read aDay

write SetDay

default 1;

end;

 

procedure TDay.SetDay (value: integer);

begin

if value in [1..31]

aDay:= value;

end;

 

( private, protected, public, published).

 

 

:

- ;

- .

( , ᒺ ) ( ).

( ᒺ ) ( ).

 

:

 

var P1, P2: TPoint;

P1:= TPoint.Create (3, 4); //P1.X=3; P1.Y=4;

P2:= TPoint.Create (5, 7); //P2.X=5; P2.Y=7;

P1:=P2; //P1.X=5; P1.Y=7;

P2.X:=10; //P2.X=10; P1.X=10;

 

ᒺ P1 ᒺ P2. ᒺ P1 .

ﳿ ᒺ, , Assign, .

 

P1.Assign(P2); // P1:=P2

 

, : .

( ).

. , : = b, b ( b , ).

 

:

 

, b .

 

ᒺ , ( ).

 

If P1=P2 then

{}

 

( ᒺ ) Equals.

 

If P1.Equals(P2) then

{}

 

ᒺ , (run-time), ᒺ . ( destructor). , Destroy.

Free, Self nil. ᒺ .

 

.

 

procedure TPoint.Free;

begin

if Self<>nil then Destroy;

end;

 

nil .

, ᒺ, , 3 :

  1. ( );
  2. (. utomatic garbage collection). ϳ ᒺ, , , . , ᒺ .
  3. ᒺ ( ᒺ), . (, ᒺ, ).

 

  1. , ;
  2. ;
  3. ;
  4. .

 





:


: 2016-07-29; !; : 464 |


:

:

.
==> ...

1625 - | 1597 -


© 2015-2024 lektsii.org - -

: 0.035 .