Einzelnen Beitrag anzeigen

Benutzerbild von geskill
geskill

Registriert seit: 17. Feb 2007
Ort: NRW
420 Beiträge
 
Delphi 2010 Professional
 
#12

AW: Interface erstellen und rückgabe

  Alt 5. Sep 2012, 17:58
Hallo,
wegen der unerwünschten Eingaben. Du könntest ja 2 Interfaces bauen:
Delphi-Quellcode:
type
  ISimpleButton = interface
    ['{B7281ABB-ED9A-4018-B651-41DFBFAE730A}']

    function GetText: WideString;
    procedure SetText(Value: WideString);

    property Text: WideString read GetText write SetText;
  end;

  IAdvancedButton = interface(ISimpleButton)
    ['{6C10BCC7-87BE-45B1-9613-44EF840DE4AA}']

    function GetWidth: Integer;
    procedure SetWidth(Value: Integer);

    function GetSimpleButton: ISimpleButton;

    property Width: Integer read GetWidth write SetWidth;
  end;
Dann könntest du von einem TAdvancedButton Objekt dir intern das IAdvancedButton speichern, extern aber nur ISimpleButton zur Verfügung stellen.

Delphi-Quellcode:
type
  TSimpleButton = class(TInterfacedObject, ISimpleButton)
  private
    FText: WideString;
  protected
    function GetText: WideString;
    procedure SetText(Value: WideString);

  public
    constructor Create;

    property Text: WideString read GetText write SetText;
  end;

  TAdvancedButton = class(TSimpleButton, IAdvancedButton)
  private
    FWidth: Integer;
  protected
    function GetWidth: Integer;
    procedure SetWidth(Value: Integer);

  public
    constructor Create;

    function GetSimpleButton: ISimpleButton;

    property Width: Integer read GetWidth write SetWidth;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TSimpleButton }

constructor TSimpleButton.Create;
begin
  inherited Create;
end;

function TSimpleButton.GetText: WideString;
begin
  Result := FText;
end;

procedure TSimpleButton.SetText(Value: WideString);
begin
  FText := Value;
end;

{ TAdvancedButton }

constructor TAdvancedButton.Create;
begin
  inherited Create;
end;

function TAdvancedButton.GetSimpleButton: ISimpleButton;
begin
  Result := Self;
end;

function TAdvancedButton.GetWidth: Integer;
begin
  Result := FWidth;
end;

procedure TAdvancedButton.SetWidth(Value: Integer);
begin
  FWidth := Value;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  AdvancedButton: IAdvancedButton;

  SimpleButton: ISimpleButton;
begin
  AdvancedButton := TAdvancedButton.Create;

  AdvancedButton.Width := 126;
  AdvancedButton.Text := 'lalala';

  ShowMessage(IntToStr(AdvancedButton.Width) + ' ' + AdvancedButton.Text);

  SimpleButton := AdvancedButton.GetSimpleButton;

  ShowMessage(SimpleButton.Text);

  SimpleButton.Text := 'blubblub';

  ShowMessage(IntToStr(AdvancedButton.Width) + ' ' + AdvancedButton.Text);
end;
Grüße
Sebastian
  Mit Zitat antworten Zitat