program SwapSample;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
type
ISwapable =
interface
['
{57064C48-8737-4A57-937D-64654F1F897C}']
procedure SetA(Value: Integer);
procedure SetB(Value: Integer);
procedure SwapWith(
var AnotherObject: ISwapable);
function GetA: Integer;
function GetB: Integer;
property A: Integer
read GetA
write SetA;
property B: Integer
read GetB
write SetB;
end;
TSprite =
class(TInterfacedObject, ISwapable)
private
FIntA: Integer;
FIntB: Integer;
public
constructor Create;
function GetA: Integer;
function GetB: Integer;
procedure SetA(Value: Integer);
procedure SetB(Value: Integer);
procedure SwapWith(
var AnotherObject: ISwapable);
end;
TOtherClass =
class(TInterfacedObject, ISwapable)
private
FIntA: Integer;
FIntB: Integer;
public
constructor Create;
function GetA: Integer;
function GetB: Integer;
procedure SetA(Value: Integer);
procedure SetB(Value: Integer);
procedure SwapWith(
var AnotherObject: ISwapable);
end;
{ TSprite }
constructor TSprite.Create;
begin
FIntA := Random(100);
FIntB := Random(250);
end;
function TSprite.GetA: Integer;
begin
Result := FIntA;
end;
function TSprite.GetB: Integer;
begin
Result := FIntB;
end;
procedure TSprite.SetA(Value: Integer);
begin
if (FIntA <> Value)
then
FIntA := Value;
end;
procedure TSprite.SetB(Value: Integer);
begin
if (FIntB <> Value)
then
FIntB := Value;
end;
procedure TSprite.SwapWith(
var AnotherObject: ISwapable);
var
tmpA, tmpB: Integer;
begin
tmpA := Self.FIntA;
tmpB := Self.FIntB;
Self.FIntA := AnotherObject.A;
Self.FIntB := AnotherObject.B;
AnotherObject.A := tmpA;
AnotherObject.B := tmpB;
end;
{ TOtherClass }
constructor TOtherClass.Create;
begin
FIntA := Random(100)
div 2;
FIntB := Random(250)
div 2;
end;
function TOtherClass.GetA: Integer;
begin
Result := FIntA * 2;
end;
function TOtherClass.GetB: Integer;
begin
Result := FIntB * 2;
end;
procedure TOtherClass.SetA(Value: Integer);
var
tmp: Integer;
begin
tmp := Value
div 2;
if (FIntA <> tmp)
then
FIntA := tmp;
end;
procedure TOtherClass.SetB(Value: Integer);
var
tmp: Integer;
begin
tmp := Value
div 2;
if (FIntB <> tmp)
then
FIntB := tmp;
end;
procedure TOtherClass.SwapWith(
var AnotherObject: ISwapable);
var
tmpA, tmpB: Integer;
begin
tmpA := Self.FIntA;
tmpB := Self.FIntB;
Self.FIntA := AnotherObject.A
div 2;
Self.FIntB := AnotherObject.B
div 2;
AnotherObject.A := tmpA;
AnotherObject.B := tmpB;
end;
var
MySprite, MyOtherObject: ISwapable;
begin
try
Randomize;
MySprite := TSprite.Create;
MyOtherObject := TOtherClass.Create;
Writeln(Format('
MySprite A: %d', [MySprite.A]));
Writeln(Format('
MySprite B: %d', [MySprite.B]));
Writeln(Format('
MyOtherObject A: %d', [MyOtherObject.A]));
Writeln(Format('
MyOtherObject B: %d', [MyOtherObject.B]));
Writeln;
Writeln('
Swapping Objects ...');
MySprite.SwapWith(MyOtherObject);
Writeln;
Writeln('
Objects swapped ...');
Writeln;
Writeln(Format('
MySprite A: %d', [MySprite.A]));
Writeln(Format('
MySprite B: %d', [MySprite.B]));
Writeln(Format('
MyOtherObject A: %d', [MyOtherObject.A]));
Writeln(Format('
MyOtherObject B: %d', [MyOtherObject.B]));
Readln;
except
on E:
Exception do
Writeln(E.ClassName, '
: ', E.
Message);
end;
end.