Delphi拾遗(8) 类事件
类的事件
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMyEvent = procedure of object; //不带参数的过程
TMyEventExt = procedure(AName: string) of object; //带参数的过程
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TMyBase = class
private
FName : string;
FAge : Integer;
FOnEvent: TMyEvent; //定义 TMyEvent 类型事件
FOnEventExt: TMyEventExt;
procedure SetAge(const AValue: Integer);
public
//创建类时进行相应的一些初始化工作
constructor Create;
procedure SetEvent1;
procedure SetEvent2;
procedure SetEventExt1(ATmp: string);
//Name属性 不可更改
property Name: string read FName write FName;
//Age属性 可以更改
property Age: Integer read FAge write SetAge;
//关联事件 发布 TMyEvent 类型事件
property OnEvent: TMyEvent read FOnEvent write FOnEvent;
property OnEventExt: TMyEventExt read FOnEventExt write FOnEventExt;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TMyBase }
constructor TMyBase.Create;
begin
FName := 'hehf';
FAge := 99; //不赋值时默认为0
FOnEvent := SetEvent1;
FOnEventExt := SetEventExt1; //这时不能带参数
end;
procedure TMyBase.SetAge(const AValue: Integer);
begin
if (AValue > 0) and (AValue < 130) then
FAge := AValue
else
FAge := -1;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
TmpMy: TMyBase;
begin
TmpMy := TMyBase.Create;
ShowMessage(IntToStr(TmpMy.Age)); // 99
TmpMy.Age := 100;
ShowMessage(IntToStr(TmpMy.Age)); // 100
TmpMy.OnEvent; //触发关联事件
TmpMy.Free;
end;
procedure TMyBase.SetEvent1;
begin
ShowMessage('Event 1'
相关文档:
一、Delphi中流的基本概念及函数声明
在Delphi中,所有流对象的基类为TStream类,其中定义了所有流的共同属性和方法。 TStream类中定义的属性介绍如下: 1、Size:此属性以字节返回流中数据大小。 2、Position:此属性控制流中存取指针的位置。 Tstream中定义的虚方法有四个: 1、Read:此方法实现将数据从流中读出。函数原形为: ......
Day 开头的函数
●
Unit
DateUtils
function DateOf(const Avalue: TDateTime): TDateTime;
描述
使用 DateOf 函数用来把一个 TDateTime 类型的变量转变成一个
只带有日期的 TDateTime 类型变量。
例如:
showmessage(DateTimetostr(dateof(now())));
你得到的是 2003/03/19
而 showmessage(DateTime ......
TDXDraw DirectDraw 和 Direct3D 组件
TDXDIB 容纳DIB(设备无关位图,Device Independent Bitmap)的组件
TDXImageList 图片列表组件
TDX3D Direct3D 组件 (和TDXDraw一起使用)
TDXSound DirectSound 组件
TDXWave 容纳 Wave(波形音频 ......
技术交流,DH讲解. 最近和肥鸟交流了下关于字符串方面的知识,而这篇文章是很久以前写的,现在发出来吧. 我们写两段代码来对比下:
第一个用Copy函数:
procedure TForm1.Button1Click(Sender: TObject);
var
a,c:Cardinal;
n:Integer;
D:Double;
i:Integer;
b:string;
begin
c:=0; ......