Ver Mensaje Individual
  #4  
Antiguo 23-09-2019
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Reputación: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola.

Otra opción es usando interfaces, un ejemplo:

Código Delphi [-]
unit uInter;  

interface

type   
  IClase2 = interface   ['{20FF5573-4A4A-4B1F-975B-817714250A03}'] // ( Ctrl+Shift+G )
    function GetValue: Integer;                  
    procedure SetValue(const  Value:Integer);
 end;

implementation

end.

Código Delphi [-]
unit Unit1; 

interface

uses  uInter; 

type
  TClase1 = class(TObject)
  private
    FValue : Integer;
  public
    Clase2: IClase2;
    constructor Create;
    property Value: Integer read FValue write FValue;
  end;

implementation

constructor TClase1.Create;
begin
  FValue := 111;
end;
end.

Código Delphi [-]
unit Unit2;

interface

uses Unit1, uInter;

type
  TClase2 = class(TInterfacedObject, IClase2)
  private
    FValue : Integer;
  public
    Clase1: TClase1;
    constructor Create;
    function GetValue: Integer;
    procedure SetValue(const  Value:Integer);
  end;

implementation

constructor TClase2.Create;
begin
  FValue := 0;
end;

function TClase2.GetValue: Integer;
begin
  Result := FValue;
end;

procedure TClase2.SetValue(const Value: Integer);
begin
  if Value <> FValue then
    FValue := Value;
end;
end.

Código Delphi [-]
program Project1; {$APPTYPE CONSOLE} {$R *.res}

uses
  System.SysUtils,  Unit1 in 'Unit1.pas',  Unit2 in 'Unit2.pas',  uInter in 'uInter.pas';

var
  Clase1: TClase1;
  Clase2: TClase2;
begin
  // Prueba Clase1
  Clase1 := TClase1.Create;
  Clase1.Clase2 := TClase2.Create;
  Clase1.Value  := 111;
  Clase1.Clase2.SetValue(222);
  Writeln(Clase1.Value, ' ', Clase1.Clase2.GetValue);

  // Prueba Clase2
  Clase2 := TClase2.Create;
  Clase2.Clase1 := TClase1.Create;
  Clase2.Clase1.Value := 333;
  Clase2.SetValue(444);
  Writeln(Clase2.Clase1.Value, ' ', Clase2.GetValue);
 
...

end.
Hay que tener en cuenta que en la interface hay que declarar funciones y procedimientos para acceder o modificar las variables de la clase.


Saludos
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita