使用過c++的人都知道在c++的類中有靜態屬性及靜態方法,為程序設計帶來很多方便.那么在Delphi中靜態屬性及靜態方法是怎么實現的呢?請看下面的實例:
unit Unit2;
interface
type
TMyClass = Class
public
{靜態過程:設置靜態屬性的值}
class procedure SetStaticMemberValue(AString: string);
{靜態函數:讀取靜態屬性的值}
class function GetStaticMemberValue: string;
end;
implementation
{在此聲明靜態屬性,這一點與c++有很大的不同}
var
AStaticMember: string;
class function TMyClass.GetStaticMemberValue: string;
begin
Result := AStaticMember;
end;
class procedure TMyClass.SetStaticMemberValue(AString: string);
begin
AStaticMember := AString;
end;
end.
那么在TMyClass中聲明的屬性及方法是否是靜態屬性或靜態方法呢?請看下面的實例:
...
uses unit2
...
procedure TForm1.Button2Click(Sender: TObject);
begin
{不需聲明TMyClass的實例,可直接設置及讀取靜態屬性的值}
TMyClass.SetStaticMemberValue('MyClass');
showmessage(TMyClass.GetStaticMemberValue);
end;
unit Unit2;
interface
type
TMyClass = Class
public
{靜態過程:設置靜態屬性的值}
class procedure SetStaticMemberValue(AString: string);
{靜態函數:讀取靜態屬性的值}
class function GetStaticMemberValue: string;
end;
implementation
{在此聲明靜態屬性,這一點與c++有很大的不同}
var
AStaticMember: string;
class function TMyClass.GetStaticMemberValue: string;
begin
Result := AStaticMember;
end;
class procedure TMyClass.SetStaticMemberValue(AString: string);
begin
AStaticMember := AString;
end;
end.
那么在TMyClass中聲明的屬性及方法是否是靜態屬性或靜態方法呢?請看下面的實例:
...
uses unit2
...
procedure TForm1.Button2Click(Sender: TObject);
begin
{不需聲明TMyClass的實例,可直接設置及讀取靜態屬性的值}
TMyClass.SetStaticMemberValue('MyClass');
showmessage(TMyClass.GetStaticMemberValue);
end;