Einzelnen Beitrag anzeigen

jenbru

Registriert seit: 23. Dez 2005
5 Beiträge
 
Delphi 7 Professional
 
#2

Re: 3 Fragen zu Interfaces in Delphi.Net

  Alt 30. Mär 2006, 10:17
Hallo,

Zitat von winx:
1. Wie kann ich in Delphi.Net ein Interface implementieren?
2. Kann ich auch mehrere Interfaces implementieren?
3. Wie "vererbt" man Interfaces
Bsp: Interface A ist eine Spezialisierung von B und B wiederum von C... schreibt man
da in A wieder alle Funktionen von B rein oder implementiert A einfach B???
zu 1)
Das ist eigentlich ganz einfach:

Delphi-Quellcode:
//Ein Interface
type
  IAnInterface = interface
    procedure AMethod;
  end;

//Eine mögliche Implementierung
type
 TAnImplementation = class(TObject, IAnInterface)
   procedure AMethod;
 end;
zu 2)
Natürlich:

Delphi-Quellcode:
//Noch ein Interface
type
  IAnotherInterface = interface
    procedure AnotherMethod;
  end;

//Klasse implementiert jetzt beide Interfaces
type
 TAnImplementation = class(TObject, IAnInterface, IAnotherInterface)
   procedure AMethod;
   procedure AnotherMethod;
 end;
zu 3)
Weder noch.
Ein Interface erbt wie eine Klasse alle Methoden vom Vorfahr-Interface. Der Vorfahr alle Interfaces in Delphi ist IInterface.
Ein Interface ist somit verpflichtet alle Methoden des Vorfahr-Interfaces zu implementieren. Das es das selbst aber nicht kann
geht die Verpflichtung auf die Klasse über, die eine Implementierung des Interface anbietet:


Delphi-Quellcode:
type
  IC = interface
    procedure C;
  end;

type
  IB = interface(IC)
    procedure B;
  end;

type
  IA = interface(IB)
    procedure A;
  end;

type
  TImpl = class(TObject, IA)
    procedure A;
    procedure B;
    procedure C;
  end;
Das steht aber auch alles in der OH von D2005 im Kapitel "Objekt-Interfaces".

Schönen Gruß,
Jens
Jens Brünnler
  Mit Zitat antworten Zitat