How to implement multiple inheritance in delphi?

前端 未结 9 735
一向
一向 2020-12-03 11:22

I\'m doing a full rewrite of an old library, and I\'m not sure how to handle this situation (for the sake of being understood, all hail the bike analogy):

I have the

相关标签:
9条回答
  • 2020-12-03 11:32

    Delphi does not support Multiple Inheritance. But classes can support / implement multiple interfaces and you can delegate interface implementation, so you can kinda simulate multiple inheritence.

    0 讨论(0)
  • 2020-12-03 11:35

    Use polymorhism to implment each 'thing' as an object hierarchy in its own right and then add object properties to that object in turn. So, create a hierarchy of wheels, and a hierarchy of bikes. Then add wheels to bikes as fields in the ancestor bike object. See below.

      TBikeWheel = class
      TBikeWheelXYZ = class( TBikeWheel ) 
    
      TBike = class
        FFrontWheel : TBikeWheel;
        property FrontWheel : TBikeWheel
          read FrontWhell  
    
      TBikeABC = class( TBike)
        constructor Create;
      end;
    
      constructor TBikeABC.Create;
      begin
        inherited;
        FFrontWheel := TBikeWheel.Create;
      end;
    
      TBikeXYZ = class( TBike)
        constructor Create;
      end;
    
      constructor TBikeXYZ.Create;
      begin
        inherited;
        FFrontWheel := TBikeWheelXYZ.Create;
      end;
    
    0 讨论(0)
  • 2020-12-03 11:41

    Sorry, Delphi does not support Multiple Inheritance.

    0 讨论(0)
  • 2020-12-03 11:42

    you can try this way, if you do not want to repeat the code several times and want a decoupled code.

    type
       TForm1 = class(TForm)
        btnTest: TButton;
        procedure btnTestClick(Sender: TObject);
       private
          { Private declarations }
       public
          { Public declarations }
       end;
    
       TBike = class
       end;
    
       IBikeWheel = interface
          procedure DoBikeWheel;
       end;
    
       TBikeWheel = class(TInterfacedObject, IBikeWheel)
       public
          procedure DoBikeWheel;
       end;
    
       IBikeWheelFront = interface
          procedure DoBikeWheelFront;
       end;
    
       TBikeWheelFront = class(TInterfacedObject, IBikeWheelFront)
       public
          procedure DoBikeWheelFront;
       end;
    
       IBikeWheelBack = interface
       end;
    
       TBikeWheelBack = class(TInterfacedObject, IBikeWheelBack)
       end;
    
       TBikeWheelFrontXYZ = class(TInterfacedObject, IBikeWheel, IBikeWheelFront)
       private
          FIBikeWheel: IBikeWheel;
          FBikeWheelFront: IBikeWheelFront;
       public
          constructor Create();
          property BikeWheel: IBikeWheel read FIBikeWheel implements IBikeWheel;
          property BikeWheelFront: IBikeWheelFront read FBikeWheelFront implements IBikeWheelFront;
       end;
    
    var
       Form1: TForm1;
    
    implementation
    
    {$R *.DFM}
    
    { TBikeWheel }
    
    procedure TBikeWheel.DoBikeWheel;
    begin
       ShowMessage('TBikeWheel.DoBikeWheel');
    end;
    
    { TBikeWheelFrontXYZ }
    
    constructor TBikeWheelFrontXYZ.Create;
    begin
       inherited Create;
       Self.FIBikeWheel := TBikeWheel.Create;
       Self.FBikeWheelFront := TBikeWheelFront.Create;
    end;
    
    { TBikeWheelFront }
    
    procedure TBikeWheelFront.DoBikeWheelFront;
    begin
       ShowMessage('TBikeWheelFront.DoBikeWheelFront');
    end;
    
    procedure TForm1.btnTestClick(Sender: TObject);
    var
       bikeWhell: TBikeWheelFrontXYZ;
    begin
       bikeWhell := nil;
       try
          try
             bikeWhell := TBikeWheelFrontXYZ.Create;
             IBikeWheelFront(bikeWhell).DoBikeWheelFront;
             IBikeWheel(bikeWhell).DoBikeWheel;
          except
             on E: Exception do
             begin
                raise;
             end;
          end;
       finally
          if Assigned(bikeWhell) then FreeAndNil(bikeWhell);
       end;                                          
    end;
    
    0 讨论(0)
  • 2020-12-03 11:45

    A variation of Brian Frost's suggestion:

      TBikeWheel = class
      TBikeWheelXYZ = class( TBikeWheel ) 
    
      TBike = class
        FFrontWheel : TBikeWheel;
      protected
        function CreateWheel: TBikeWheel; virtual;
      public
        property FrontWheel : TBikeWheel
          read FrontWheel  
      end;
    
      TBikeABC = class( TBike)
      protected
        function CreateWheel: TBikeWheel; override;
      end;
    
      function TBikeABC.CreateWheel: TBikeWheel;
      begin
        result := TBikeWheel.Create;
      end;
    
      TBikeXYZ = class( TBike)
      protected
        function CreateWheel: TBikeWheel; override;
      end;
    
      function TBikeXYZ.CreateWheel: TBikeWheel;
      begin
        result := TBikeWheelXYZ.Create;
      end;
    
    0 讨论(0)
  • 2020-12-03 11:53

    Basically - you CAN'T. Delphi does not support multiple inheritance.

    So left with that dilemma, the question is: could you possibly refactor that library in such a way that you can get away with using interface? Is the multiple inheritance mostly about functions and methods? If so - use interfaces. Delphi can support multiple interfaces on a class.

    If the multi-inheritance is more about inheriting actual functionality in the classes, then you're probably looking at a bigger scale refactoring, I'm afraid. You'll need to find a way to break up those functional dependencies in such a way you can make it inherit from a single base class, possibly with some additional interfaces thrown in.

    Sorry I can't provide an easy answer - that's just the reality of it.

    Marc

    0 讨论(0)
提交回复
热议问题