Delphi 知识点总结:
1)stored,default 关键字修饰的属性与普通属性的区别
The optional stored, default, and nodefault directives are called storage specifiers.They have no effect on program behavior, but control whether or not to save the values of published properties in form files.
The stored directive must be followed by True, False, the name of a Boolean field, or the name of a parameterless method that returns a Boolean value.
If a property has no stored directive, it is treated as if stored True were specified.
f such a property is declared without default or nodefault, it is treated as if nodefault
If a property's current value is different from its default value (or if there is no default value) and the stored specifier is True, then the property's value is saved. Otherwise, the property's value is not saved.
Property values are not automatically initialized to the default value. That is, the default directive controls only when property values are saved to the form file, but not the initial value of the property on a newly created instance.
2)Delphi 类必须包含哪些成员吗?如构造和析构函数等
create
This constructor should be called to create an instance of a component. This constructor should not normally be called if the component is placed visually on a form.
destroy
The Destroy destructor should never be called directly. To destroy a component created with Create, call the Free method.
delphi的构造函数的定义是:
constructor create;
delphi的析构函数的定义是:
destructor destroy;
析构函数是不能重载的,但是构造函数是可以重载的。
构造函数在重载的时候要在后面加“overload”,例如:
constructor create;overload;
constructor create(i:integer);overload;
注意,只有两个构造函数以上才叫重载,只有一个就不用“overload;”了。
默认的构造函数是:constructor create; 如果有重载的话,那么默认的构造函数后面也要加overload,正象上面的例子一样。
delphi构造函数在类外定义在什么位置呢?在implementation的后面。下面给出一个实例,可以从这个实例中看出构造函数的定义:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TMyForm1 = class(TForm1) //自定义一个TMyForm1类
public
constructor Create; overload; //构造函数有重载
constructor Create(I: Integer); overload; //重载一个构造函数
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
constructor TMyForm1.Create; //这里定义构造函数
begin
inherited Create(nil); //inherited 表示调用父类的构造函数
end;
constructor TMyForm1.Create(I: Integer);
begin
inherited Create(nil);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
A: TMyForm1;
begin
A := TMyForm1.Create(1);
A.Show;
end;
end.
例二:
Delphi中重载Create,Destroy方法
2008-05-30 11:31
(出处:http://www.ithr.org.cn/blogs/index.php?blog=17&title=delphiacse_effcreatea_sas&c=1)
例:
声明:在TModifyTaskForm类中
public
constructor Create(AOwner: TComponent; AUserId: Integer); reintroduce; overload;
destructor Destroy; reintroduce;
实现:
constructor TModifyTaskForm.Create(AOwner: TComponent; AUserId: Integer);
begin
inherited Create(AOwner);
处理重载内容;
end;
destructor TModifyTaskForm.Destroy;
begin
inherited Destroy;
处理重载内容;
end;
3)virturl,dynamic,override(重写),overload(重载),reintroduce 的区别
通过覆盖使一方法在不同的派生类间表现出不同的行为。Object Pascal 中能被覆盖的方法是在
声明时被标识为 virtual 或 dynamic 的方法。为了覆盖一个方法,在派生类的声明中用
override 代替 virtual 或 dynamic。
后面又说到:
如果用 virtual 或 dynamic 替换 override,将是建立新的方法而不是对祖先的方法进行覆盖。
同样,在派生类中如果企图对一个静态方法进行覆盖,在派生类中如果企图对一个静态方法进行
覆盖,在新对象中的方法完全替换在祖先类中的同名方法。
下面是关于 reintroduce 的:
有时候,需要在派生类中增加一个方法,而这个方法的名称与祖先类中的某个方法名称相同。在
这种情况下,没必要覆盖这个方法,只要在派生类中重新声明这个方法。但在编译时,编译器就
会发出一个警告,告诉你派生类的方法将隐藏祖先类的同名方法。要解决这个问题,可以在派生
类中使用 reintroduce 指示符。
用override重载的方法可以在子类的同名方法中使用inherited指示字调用父类的同名方法,而静态重载的是不可以的,reintroduce指示字的存在使你可以用这种方法来使用父类中的同名方法:TprntObject(self).OurProcedure,静态重载的是不可以这样的。