我是Delphi菜菜,少见多怪!大家帮我看看。type
TForm1 = class(TForm)
private
Flag: Boolean;
public
constructor Create(AOwner: TComponent); override;
end;var
Form1: TForm1;implementationconstructor TForm1.Create(AOwner: TComponent);
begin
Flag := True;
inherited Create(Application);
end;end.Flag是TForm1的成员变量, 可是在TForm1.Create(AOwner: TComponent)函数中:
在 "TForm1实例化之前" 就对 "Flag进行了付值", 也就是说在为TForm1分配内存空间之前,Flag就已经有了内存空间. 是怎么回事? Delphi中的类和对象怎么区分?
TForm1 = class(TForm)
private
Flag: Boolean;
public
constructor Create(AOwner: TComponent); override;
end;var
Form1: TForm1;implementationconstructor TForm1.Create(AOwner: TComponent);
begin
Flag := True;
inherited Create(Application);
end;end.Flag是TForm1的成员变量, 可是在TForm1.Create(AOwner: TComponent)函数中:
在 "TForm1实例化之前" 就对 "Flag进行了付值", 也就是说在为TForm1分配内存空间之前,Flag就已经有了内存空间. 是怎么回事? Delphi中的类和对象怎么区分?
应该理解为:先为Form1分配了内存空间,然后才调用构造函数的。
调用的。Delphi的这种做法有点象是给一个类的成员变量付值,而不是一个
对象!我觉得。
C++不也是么?
inherited Create(Application);
通常,我们不这样写的,是这样写的
inherited Create(AOwner);
Flag := True;form不一定用Application创建,所以Application的传递有问题。
类在创建的时候,已经为变量、方法分配了存储空间,否则Create的时候,Self隐含参数就没有意义了。
TForm1.Create(Application);