我写了一个组件,继承自TComponent,重载了构造函数(实际上是另外定义了一个构造函数),因为参数不同,不是
constructor create(Owner:TComponent);override;
的形式,而是
constructor create(Owner:TComponent;a:TMyType);//该函数实现部分有这么一句话:inherited create(owner);这种形式在一般的类对象创建时候只要参数传对了就没问题了。现在,我把这个类注册为组件,在运行的时候,它却不进入构造函数体内执行,何故?
解决方案 »
- 为什么这三行代码不起作用?
- 老是报错“查询过于复杂”,access数据库ado。
- 请问哪有cxgrid和dxdbgrid控件下载?
- 怎么样得到一个excel表的sheetname啊?或知道它是否存在特定的sheetname啊?
- Pagecontrol如何纵向显示?
- 请教DELPHI+TIMER的问题,谢谢
- 在Delphi中如何设置注册表中键值项为只读属性,写属性?
- CSDN商城支付配送开始多样化?什么意思是共享软件销售的支付方式吗?
- 使用第三方控件的时候能不能象原有控件一样直接使用??
- 新手菜问题。。。大神在哪里??
- 怎样计算字符串(含中英文的)的长度,如“a我”要算成长度为2
- 如何获得一张图片的大小和文件名,有这样现成的函数吗?
一般来做要用到的对像都在构造函数里先CREATE
不错,我就是在构造函数里面create几个成员的实例,这个具体的内容也没什么特别的,关键是它根本就不到这个构造函数体内执行,至于什么内容倒在其次了。稍等,我贴上来。
public
Constructor Create(PADOConnect:TADOConnection;PAppName:String);
//这里不能override,因为这个函数结构不同于其祖先类TComponent的构造函数*************实现部分****************************************************
constructor TUser.Create(PADOConnect: TADOConnection;PAppName:String);
begin
AppName:=PAppName;
ADOC:= PADOConnect;
Qry_User:=TADOQuery.Create(nil);
Qry_User.Connection:= ADOC;
.......
end;
constructor Create(Owner:TComponent;a:TMyType); reintroduce; overload;
reintroduce; overload; 都没有加,因为都没有效果。
constructor Create(Owner:TComponent;a:TMyType); reintroduce;
constructor Create(Owner: TComponent); override;
因为跟踪一下源码
当窗体创建时,
建立每一个组件时会调用
function TReader.ReadComponent(Component: TComponent): TComponent;
它会调用
CreateComponent procedure CreateComponent;
var
ComponentClass: TComponentClass;
begin
try
ComponentClass := FindComponentClass(CompClass);
Result := nil;
if Assigned(FOnCreateComponent) then
FOnCreateComponent(Self, ComponentClass, Result);
if Result = nil then
begin
Result := TComponent(ComponentClass.NewInstance);
if ffInline in Flags then
begin
Include(Result.FComponentState, csLoading);
Include(Result.FComponentState, csInline);
end;
try
Result.Create(Owner);
except
Result := nil;
raise;
end;
end;
Include(Result.FComponentState, csLoading);
except
if not Recover(Result) then raise;
end;
end;都在classes.pas内
也就是说在这块
Result.Create(Owner);
如果不用override,
则只能进TComponent.Create里
为什么必须传一个a: TMyType呢
reintroduce; overload 我联合用了也无效。 xzgyb(老达摩) :现在我不能调试。不过,我曾经改过一个窗口(TForm也是TComponent的派生类)的constructor函数,这样用也没有问题;使用一般类(不注册为组件)的时候,也没问题。largewang(老王) :提提建议。谢!
那是因为你在程序里自己调用的
TForm1.Create(Self, 3)这类的
而注册为组件
是由
delphi在创建组件时调用的
Result.Create(Owner);
就是这种形式
a:TMyType
完全可以作为一个组件的属性,
不一定非得这样
constructor create(Owner:TComponent;a:TMyType)