編程生活

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            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 閱讀(549) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 新密市| 平顺县| 秭归县| 社旗县| 四会市| 双流县| 富锦市| 哈巴河县| 广安市| 鸡西市| 湟中县| 枞阳县| 当雄县| 阜平县| 云安县| 松潘县| 怀宁县| 普宁市| 罗田县| 桓仁| 辽宁省| 莆田市| 小金县| 叙永县| 尼木县| 永仁县| 乐业县| 云梦县| 兴城市| 雅江县| 滨州市| 吉木乃县| 桃园市| 灵山县| 固镇县| 板桥市| 祁门县| 吴川市| 清原| 社会| 商水县|