إستفسار حول OOP Interface in Delphi ؟
#3
بارك الله فيك اخي سفيان على الرد (كم تسعدنا بردودك القيمة)

نعم كما ذكرت, سبب المشكلة هو أنه لم أقم بعمل Implementation لـ 3 methodes موجودة  في Interface المسمات IInterface والتي قمت بعمل وراثة منها لـ interface الخاصة بي التي سميتها IMyInterface.

وهذا الكود الموضح للعملية : 
كود :
IMyInterface = Interface(IInterface)     function GetAdd(pX , pY: integer) : integer;   End;

إذا في هذه الحالة اصبحت IMyInterface تحتوي على 4 methodes واحدة التي كتبتها GetAdd و 3 methodes جاءؤ عن طريقة الوراثة من [size=small]IInterface.[/size]

في هذه الحالة ايي كلاس تقوم بعمل Implementation لل IMyInterface يجب عليها تعريف 4 methodes يعني كتابة body الخاص بهم.

وعليه الحل رقم 1 يكون كما يلي :

كود :
unit uMyUnit; interface type   IMyInterface = Interface(IInterface)     function GetAdd(pX , pY: integer) : integer;   End;   TClassA = class(TObject)     private       FX : integer;       FY : integer;     public       constructor Create(pX, pY : integer);       function GetName: String;       function GetProduit : integer;   end;   TApp = class(TClassA ,IMyInterface)     public       constructor Create(pX, pY: integer);       function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;       function _AddRef: Integer; stdcall;       function _Release: Integer; stdcall;       function GetAdd(pX , pY: integer) : integer;   end; implementation { TClassA } constructor TClassA.Create(pX, pY: integer); begin   FX:= pX;   FY:= pY; end; function TClassA.GetName: String; begin   Result := 'TClassA'; end; function TClassA.GetProduit: integer; begin   Result := FX * FY; end; { TApp } constructor TApp.Create(pX, pY: integer); begin   inherited Create(pX, pY); end; function TApp.GetAdd(pX, pY: integer): integer; begin   Result := FX + FY; end; function TApp.QueryInterface(const IID: TGUID; out Obj): HResult; begin   if GetInterface(IID, Obj) then     Result:= S_OK   else     Result:= E_NOINTERFACE; end; function TApp._AddRef: Integer; begin   Result:= -1; end; function TApp._Release: Integer; begin   Result:= -1; end; end.

والحل رقم 2 يكون بستغلال الـ Class TInterfacedObject التي قامت مسبقا بعمل implementation لـ IInterface :
لذلك يجب تغيير هذا التعريف :
كود :
TClassA = class(TObject)
 بهذا : 
كود :
TClassA = class(TInterfacedObject)

ويبقى كل شيء على ماهو عليه.
الكود الكامل للحل رقم 2 :

كود :
unit uMyUnit; interface type   IMyInterface = Interface(IInterface)     function GetAdd(pX , pY: integer) : integer;   End;   TClassA = class(TInterfacedObject)     private       FX : integer;       FY : integer;     public       constructor Create(pX, pY : integer);       function GetName: String;       function GetProduit : integer;   end;   TApp = class(TClassA ,IMyInterface)     public       constructor Create(pX, pY: integer);       function GetAdd(pX , pY: integer) : integer;   end; implementation { TClassA } constructor TClassA.Create(pX, pY: integer); begin   FX:= pX;   FY:= pY; end; function TClassA.GetName: String; begin   Result := 'TClassA'; end; function TClassA.GetProduit: integer; begin   Result := FX * FY; end; { TApp } constructor TApp.Create(pX, pY: integer); begin   inherited Create(pX, pY); end; function TApp.GetAdd(pX, pY: integer): integer; begin   Result := FX + FY; end; end.

ملاحظة : الحل رقم 2 إستنتجته لوحدي من التجارب التي اجريتها واره افضل من الحل الأول , ما تعليقكم عليه؟؟
الرد


الردود في هذا الموضوع
RE: إستفسار حول OOP Interface in Delphi ؟ - بواسطة MasteranyOne - 28-07-2020, 04:33 PM

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم