AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Interface erstellen und rückgabe

Ein Thema von EWeiss · begonnen am 2. Sep 2012 · letzter Beitrag vom 5. Sep 2012
 
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, 16: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
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 19:58 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz