gpt4 book ai didi

delphi - Delphi XE2 中的类助手和类拦截器

转载 作者:行者123 更新时间:2023-12-03 15:19:11 24 4
gpt4 key购买 nike

我有一段代码,如下所示:

界面部分:

TControlOrganizer = class(TComponent)  // a kind of List of TControl
private
FList : TList; //Handling list of TControl
..............
end;

TControlHelperAdd = class // Just for separating value entered by user and value produced by code
private
FOrganizer : TControlOrganizer;
FOrigValue : String;
FUserValue : String;
end;

TControlHelper = class helper for TControl //Basic helper for TControl
private
class var
FAddProp : TControlHelperAdd;
protected
procedure SetOrganizer(Value : TControlOrganizer);
function getOrganizer:TControlOrganizer;
public
class Constructor doinit;
class Destructor deinit;
procedure OrganizerChanged;virtual;
published
Property Organizer:TControlOrganizer read getOrganizer write setOrganizer; //Problem No.1
end;

//Helper for CustomEdit inherited from TControlHelper
TCustomEditHelper = class helper(TControlHelper) for TCustomEdit
protected
procedure OrganizerChanged;override;
end;
//TEdit : interceptor
TEdit= class(StdCtrls.TEdit)
protected
procedure Change;override; //problem No.3
end;

实现部分:

{TControlHelper}

class Constructor TControlHelper.doinit;
begin
FAddProp :=TControlHelperAdd.Create;
end;

class Destructor TControlHelper.deinit;
begin
FreeAndNil(FAddProp);
end;

procedure TControlHelper.OrganizerChanged;
begin
end;

procedure TControlHelper.SetOrganizer(Value : TControlOrganizer);
begin
if FAddProp.FOrganizer<>value then begin
if assigned(FAddProp.FOrganizer) then begin
FAddProp.FOrganizer.remove(self);
end;
FAddProp.FOrganizer:=value;
if assigned(FAddProp.FOrganizer) then begin
FAddProp.FOrganizer.Add(self);
end;
OrganizerChanged; //Problem No.2
end;
end;

function TControlHelper.getOrganizer:TControlOrganizer;
begin
result:=FAddProp.FOrganizer;
end;

{TCustomEditHelper}
procedure TCustomEditHelper.OrganizerChanged; //problem No.2
begin
if assigned(FAddProp) then begin
if assigned(Organizer) then begin
FAddProp.FOrigValue:=Text;
FAddProp.FUserValue:=Text;
end;
end;
end;

{TEdit = Interceptor}
procedure TEdit.Change; //Problem No.3
begin
inherited Change;
FAddProp.FUserValue:=Text;
end;

我发现了 3 个我自己无法解决的问题。

问题 1:已发布的属性不会显示在对象检查器中。我想我犯了一个错误,忘记了一些东西。

问题2:如上所示,类帮助器中的重写方法没有触发(不起作用)。

问题3:如上所示,拦截器类中的重写方法没有触发(不起作用)。

谁能帮我找出问题所在吗?感谢您的帮助,并对我糟糕的英语表示歉意。

最佳答案

问题1

A published property in a helper does not show up in the Object Inspector.

没错。在助手中声明的属性永远不会出现在对象检查器中。

问题2

Virtual methods have no effect in class helpers.

这又是设计使然。尽管编译器允许您在帮助程序中定义虚拟方法,但它没有任何效果。为了触发 TCustomEditHelper.OrganizerChangedTCustomEditHelper 需要成为目标对象的事件助手。

问题3

The Change method for my intercepted TEdit class does not fire.

那是因为您没有实例化拦截的TEdit。如果实例化正确的类,即您定义的拦截器类,该方法将触发。

关于delphi - Delphi XE2 中的类助手和类拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13806338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com