

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;
TMyclass = class //定义一个类
public //类方法可见
class procedure Myproc;
constructor create; {这里不用‘create'这个字符串作类方法的名称也可以,
无论什么名,constructor关键字决定了它是构造函数}
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
myclass:TMyclass; //声明类变量
begin
TMyclass.Myproc; //类要创建后才能使用,这里为什么还没创建就能用呢?因为这个“过程”是在本单元中,显示:ok 。
myclass:=TMyclass.create; //构造函数内部调用类方法,显示:ok
myclass.Myproc; //对象调用类方法,显示:ok
myclass.Free; //类使用完要释放
end;
{ TMyclass }
constructor TMyclass.create;
begin
inherited; //inherited关键用来继续父类的方法,这里是继承了父类的构造函数
Myproc; //在继承了父类的构造函数后再加上点其它功能
end;
class procedure TMyclass.Myproc;
begin
ShowMessage('OK');
end;
end.