How to inherit if TForm child class?

I admit that this is the first time I use inheritance, so I may have even chosen the wrong path, so I ask you here.

I wrote a message handler in my delphi application to catch messages from WSAAsyncSelect ()

procedure FormMain.MessageHandler(var Msg:Tmessage);
begin
  case WSAGetSelectEvent(MSG.LParam) of
    FD_READ: //OnSocketRead(MSG.WParam);
    FD_CLOSE: //OnSocketClose(MSG.WParam);
  end;
end;

The problem is that OnSockerRead and OnSocketClose are functions in another class.

I want to establish a good relationship between classes so that a class with these two functions can access the parent, but at the same time things that should be private to other classes.

Please show me an example of how to do this because I don’t know whether it is better to be abstract or inherited, since I have never used both of them. I want to make my code more OO.

Thanks!

+3
2

, , . , , SocketRead SocketClose, . , . - , . :

unit MainFormShared;
interface
type
  IMainFormShared = interface
    ['{A2C624D5-DDCF-49D6-8B03-791BA0B79A42}']
    procedure SocketRead(var Handle : Integer);
    procedure SocketClose(Var Handle : Integer);
  end;
implementation
end.

(, ):

type
  tMainForm = class(TForm,IMainFormShared)
  :
  private
    procedure SocketRead(var Handle : Integer);
    procedure SocketClose(Var Handle : Integer);
  end;

:

procedure TParentForm.MessageHandler(var Msg:Tmessage);
var
  fMainFormShared : IMainFormShared;
begin
  case WSAGetSelectEvent(MSG.LParam) of
    FD_READ: 
      if Supports(Application.MainForm, IMainFormShared,fMainFormShared) then  
        fMainFormShared.SocketRead(Msg.WParam);
    FD_CLOSE: //OnSocketClose(MSG.WParam);
      if Supports(Application.MainForm, IMainFormShared,fMainFormShared) then  
        fMainFormShared.SocketClose(Msg.WParam);
  end;
end;
+7

, , MainForm, .

- . .

, , , OtherClass () , , , .

+1

Source: https://habr.com/ru/post/1712582/


All Articles