I admit this is the first time I use inheritance,so I might even have choosen the wrong way,that's why I'm here asking you.
我承认这是我第一次使用继承,所以我甚至可能选择了错误的方式,这就是我在这里问你的原因。
I wrote a Message Handler in my delphi application to catch the messages from WSAAsyncSelect()
我在delphi应用程序中编写了一个Message Handler来捕获来自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.
问题是OnSockerRead和OnSocketClose是另一个类中的函数。
I want to make a good relationship between the classes so the class with those two functions can access it's parent ,but in the same time the things to be private to other classes.
我希望在类之间建立良好的关系,以便具有这两个函数的类可以访问它的父级,但同时将其他东西私有化。
Please show me an example how should I do it,because I don't know if it's better to be abstract or inherited since I have never used both of them.I want to make my code more OO.
请给我一个例子,我该怎么做,因为我不知道抽象或继承是否更好,因为我从未使用过它们。我想让我的代码更加OO。
Thank you!
2 个解决方案
#1
One thing you can do is to use interfaces to gain access to main form functionality. For example, lets say that you want to call either SocketRead or SocketClose which are on the main form from your child form. you COULD just use mainform in the implementation of the unit, but I try to avoid these types of circular references. The other option is to create a new unit to contain a shared interface and use it by both the main form and the child unit. For example:
您可以做的一件事是使用接口来访问主窗体功能。例如,假设您要调用子表单中主窗体上的SocketRead或SocketClose。你可以在单元的实现中使用mainform,但我尽量避免使用这些类型的循环引用。另一种选择是创建一个新单元以包含共享接口,并由主窗体和子单元使用它。例如:
unit MainFormShared;
interface
type
IMainFormShared = interface
['{A2C624D5-DDCF-49D6-8B03-791BA0B79A42}']
procedure SocketRead(var Handle : Integer);
procedure SocketClose(Var Handle : Integer);
end;
implementation
end.
your main form would implement this interface (ok to keep the implementation private):
您的主窗体将实现此接口(确保将实现保持为私有):
type
tMainForm = class(TForm,IMainFormShared)
:
private
procedure SocketRead(var Handle : Integer);
procedure SocketClose(Var Handle : Integer);
end;
From the parent object in your inheritance chain you can implement your message handler like so:
从继承链中的父对象,您可以像这样实现消息处理程序:
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;
#2
I don't think inheritance is the answer here, unless that OtherClass can be derived from MainForm, but that looks doubtful.
我不认为继承是这里的答案,除非可以从MainForm派生出OtherClass,但这看起来很值得怀疑。
One way to open up access is to put both classes in the same Unit. That gives them instant access to each others implementation details.
打开访问权限的一种方法是将两个类放在同一个单元中。这使他们可以即时访问彼此的实施细节。
But maybe you are trying to hard here, if OtherClass in it's own (small) unit that nobody else is USES then it won't be that bad to make those functions public.
但也许你在这里努力,如果其他人没有使用其自身(小)单位的其他类别,那么将这些功能公之于众并不会那么糟糕。
#1
One thing you can do is to use interfaces to gain access to main form functionality. For example, lets say that you want to call either SocketRead or SocketClose which are on the main form from your child form. you COULD just use mainform in the implementation of the unit, but I try to avoid these types of circular references. The other option is to create a new unit to contain a shared interface and use it by both the main form and the child unit. For example:
您可以做的一件事是使用接口来访问主窗体功能。例如,假设您要调用子表单中主窗体上的SocketRead或SocketClose。你可以在单元的实现中使用mainform,但我尽量避免使用这些类型的循环引用。另一种选择是创建一个新单元以包含共享接口,并由主窗体和子单元使用它。例如:
unit MainFormShared;
interface
type
IMainFormShared = interface
['{A2C624D5-DDCF-49D6-8B03-791BA0B79A42}']
procedure SocketRead(var Handle : Integer);
procedure SocketClose(Var Handle : Integer);
end;
implementation
end.
your main form would implement this interface (ok to keep the implementation private):
您的主窗体将实现此接口(确保将实现保持为私有):
type
tMainForm = class(TForm,IMainFormShared)
:
private
procedure SocketRead(var Handle : Integer);
procedure SocketClose(Var Handle : Integer);
end;
From the parent object in your inheritance chain you can implement your message handler like so:
从继承链中的父对象,您可以像这样实现消息处理程序:
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;
#2
I don't think inheritance is the answer here, unless that OtherClass can be derived from MainForm, but that looks doubtful.
我不认为继承是这里的答案,除非可以从MainForm派生出OtherClass,但这看起来很值得怀疑。
One way to open up access is to put both classes in the same Unit. That gives them instant access to each others implementation details.
打开访问权限的一种方法是将两个类放在同一个单元中。这使他们可以即时访问彼此的实施细节。
But maybe you are trying to hard here, if OtherClass in it's own (small) unit that nobody else is USES then it won't be that bad to make those functions public.
但也许你在这里努力,如果其他人没有使用其自身(小)单位的其他类别,那么将这些功能公之于众并不会那么糟糕。