編程生活

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            113 隨筆 :: 0 文章 :: 18 評論 :: 0 Trackbacks
          使用過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;
          posted on 2009-01-07 12:32 wilesun 閱讀(554) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 句容市| 铜梁县| 台南市| 平果县| 开鲁县| 托克托县| 滨州市| 林周县| 丽水市| 南漳县| 天祝| 屏南县| 观塘区| 从化市| 福州市| 英山县| 泾源县| 西乌| 布尔津县| 比如县| 汨罗市| 敦化市| 宜城市| 绥滨县| 同仁县| 永嘉县| 玛沁县| 县级市| 深州市| 汕头市| 中超| 旬邑县| 浦东新区| 平果县| 景德镇市| 长白| 历史| 云安县| 三明市| 太保市| 浦江县|