在TObject类定义中有若干型如如下的函数:class function classname:shortstring;
class function .....
等等...请问在函数前加class是什么意思?有何用处?
为什么过程前没有class ?过程procedure和属性property有什么不同之处?

解决方案 »

  1.   

    类函数,
    在一个方法前加上关键字class,使得方法向其他通常的过程和函数一样调用而不需要生成一个包含这个方法的类的实例,这个功能是从C++的static函数借鉴来的。要小心,不要让一个类方法依赖于任何实例信息,否则编译时将出错。
    这么说可能不太清楚看object里的定义吧类方法类方法就是作用于类而不是作用于对象的方法(除构造器外)。类方法的定义必需以保留字class开始。例如,type  TFigure = class  public    class function Supports(Operation: string): Boolean; virtual;    class procedure GetInfo(var Info: TFigureInfo); virtual;    ...  end;类方法的定义声明也必需以保留字class开始。例如,class procedure TFigure.GetInfo(var Info: TFigureInfo);begin  ...end;在类方法的定义声明中,标识符Self表示方法被调用处的类(可以是方法定义所在的类的后裔类)。如果在类C中调用类方法,那么Self就是类C的类型。因此,这时不能用Self对域、属性和一般的对象方法进行访问,但可以用它调用构造器和其他的类方法。类方法可以通过类引用或对象引用被调用。当类方法通过对象引用被调用时,对象的类成为Self的值。
      

  2.   

    就是类的方法,就是直接用类名称就可以访问的函数或者过程.
    比如:  caption := TButton.ClassName;
      

  3.   

    在C++/C#中可以在成员函数前面加static关键字,那样的函数是静态成员函数,无法和非静态成员函数共享类里面的非静态数据。但是可以不实例化而直接调用该函数。Object Pascal中成员函数的class前缀,用来表示该成员函数是一个静态成员函数。那么你可以不用实例化而访问类的方法。例如
    type TMyClass=class
       class procedure HeyGuys;
       end;
    implementationclass procedure TMyClass.HeyGuys;
    begin
      ShowMessage('Hey Guys!');
    end;当你使用该成员函数的时候,你有两种方法来调用1。直接调用静态方法 TMyClass.HeyGuys;
    2。调用实例化后对象的该方法(实际上还是调用的TMyClass.HeyGuys)。
       var myClass:TMyClass;
       begin
          myClass:=TMyClass.Create;
          myClass.HeyGuys;
          FreeAndNil(myClass);
       end;
      

  4.   

    类方法的使用  类方法(Class methods)是一类特殊的方法,它们在声明时要以class开头  type  TFigure = class  public  ...  class procedure GetInfo(varInfo:TFigureInfo); virtual;  ...  end;  实现时也以class开头  class procedureTFigure.GetInfo(varInfo:TFigureInfo);  begin  ...  end;  (具体意义请自行查看帮助)  乍一看好象平时没有遇到过这个东东,也没有看到过谁用过这个东东,好象这个东东也没有什么大作用,其实不然……  比如我们有时为输入密码或其他常用数据专门做一个form,但由于其代码都在form定义的unit里面,所以在使用时仅仅需要几行代码,比如  with TfrmPassword.Create(nil) do  try  ShowModal;  finally  Free;  end;  虽然这样的代码已经很简洁,但如果写个十七八个还是很讨厌的。利用类方法可以使其更简洁!一行足以……  TfrmPassword = class(TForm)  ...  public  { Public declarations }  class function Execute:TModalResult;  end;  ...  class function TfrmPassword.Execute:TModalResult;  begin  with TfrmPassword.Create(nil) do  try  Result := ShowModal;  finally  Release; //注意此处必须为release不能为free!  end;  end;  然后只用一行  TfrmPassword.Execute;  即可直接完成调用……是否很爽^_^
     
      

  5.   

    TO hkbarton(宁静至远||淡泊明志)  
     "类函数,通过类名直接调用,不用通过对象调用"  
    TO thh820630(空吻)  
    "是类函数 只能由类调用 不能由对象调用"类函数也可以由对象调用。
    TO myling(阿德) 
    能否将类方法中的self再解释更清楚一下呢?
    C++中不能在类方法中访问this,而
    OP中却可以访问self,但不能访问属性、非类方法等。
    如在类方法中使用integer(self);的表达式。
    我感到有些困惑,请不吝执教。
      

  6.   

    class + function / procedure 表示类方法,不需要创建类的对象就可以直接调用的方法,和c++ 、java中static的意思是一致的!procedure & property 的区别:
    他们都是类的成员,所不同的是procedure是类的方法,property 是类的属性,我想属性和方法你还能分辨清楚吧!好了,我觉得你应该多看看一些object pascal的基础书籍!
      

  7.   

    我想楼主肯定是在阅读VCL源码.如前面各位高手所说的,她类似C++中的static函数
      

  8.   

    myling(阿德)的这句话
    类函数,
    在一个方法前加上关键字class,使得方法向其他通常的过程和函数一样调用而不需要生成一个包含这个方法的类的实例,这个功能是从C++的static函数借鉴来的。
    -------------------------------------------------------------
    要小心,不要让一个类方法依赖于任何实例信息,否则编译时将出错。
    -------------------------------------------------------------
    我觉得这句话,最关键。也是Class方法的特点。
      

  9.   

    Delphi中对象类型的变量实质是一个指向类在内存中的实例的指针,本身已经是个指针了.
    所以integer(self),实际上就跟c++中(int)this一样的。将指针转换为整形。
      

  10.   

    Delphi 开发人员指南中有解释。就是类方法可以想像为C++中的静态方法在使用的时候可以不用实例来引用。因为它们已随类存在。
      

  11.   

    TO madyak(无天) :
      with TfrmPassword.Create(nil) do
      try
      Result := ShowModal;
      finally
      Release; //注意此处必须为release不能为free!<-----此处为何不能用free?
      end;
    请解释一下?
    TO myling(阿德):
    "在类方法的定义声明中,标识符Self表示方法被调用处的类(可以是方法定义所在的类的后裔类)"如何理解?
    如果定义类以及类函数:
    TA = class
     class function f1: integer;
    end;class function TA.f1: integer;
    begin
     result := Integer(self);
    end;
    定义变量t1: TA;
    调用TA.f1时得到的结果并不等于(PInteger(integer(t1))^),后者是类的VMT
      

  12.   

    5555555555~~~~~~~~~~~~~~~~~~~我前面写了那么多,你们当我空气类的静态方法只可以访问类的静态数据。 至少在C++中如此,Delphi中不支持静态数据类的实例化并没有将方法的代码在内存中COPY一份,仅仅是些字段还有一些RTTI的信息。而类类本身(例如TClass),可能根本就没有实例化过,所以类的成员字段没有分配内存,为了防止运行时出错,编译器就不让你访问类的字段。例如:
    type TMyClass=class
       private
         field1:string;
         field2:integer;
       public
         class procedure foo;
       end;
    class procedure TMyCLass.foo;
    var field3:integer;
    begin
      field1:='test';//编译错误
      field2:=2;//编译错误
      field3:=3;//这里可以通过编译,因为这个变量的生存期在foo里面,而不是存在类的实例中。
    end;
    在这里面,是不允许通过编译的,因为这个类可能根本就没有被实例化,哪里来的field1,field2呢?或者由多个实例,你知道你访问的是哪个实例的字段呢?所以干脆不允许静态方法访问成员字段。
      

  13.   

    Destroys the form and frees its associated memory.Delphi syntax:procedure Release;DescriptionUse Release to destroy the form and free its associated memory.Release does not destroy the form until all event handlers of the form and event handlers of components on the form have finished executing. Release also guarantees that all messages in the form's event queue are processed before the form is released. Any event handlers for the form or its children should use Release instead of Free (Delphi) or delete (C++). Failing to do so can cause a memory access error.Note: Release returns immediately to the caller. It does not wait for the form to be freed before returning.
    Destroys an object and frees its associated memory, if necessary.Delphi syntax:procedure Free;C++ syntax:__fastcall Free();DescriptionUse Free to destroy an object. Free automatically calls the destructor if the object reference is not nil. Any object instantiated at runtime that does not have an owner should be destroyed by a call to Free so that it can be properly disposed of and its memory released. Unlike Destroy, Free is successful even if the object is nil; so if the object was never initialized, Free won抰 result in an error.When you call Free for a component, it calls Free for all components that it owns梩hat is, all components in its component list. Since a form owns all the controls and other components that are created on it in design mode, those components are automatically freed when the form is freed. By default, all forms are owned by the Application object; when the application terminates, it frees the Application object, which frees all forms. For objects that are not components, or for components created with a nil owner, be sure to call Free after you are finished with them; otherwise the allocated memory will not be usable until after the application terminates.Warning: Never explicitly free a component within one of its own event handlers or the event handler of a component it owns or contains. For example, don抰 free a button, or the form that owns the button, in its OnClick event handler.To free a form, call its Release method, which destroys the form and releases the memory allocated for it after all its event handlers and those of the components it contains are through executing.
      

  14.   

    呵呵,没想到这个贴子还有下文
    不过都让Eastunfail(恶鱼杀手)说完了  :-)
      

  15.   

    to zhxfzhxf1(zhxfzhxf1):
     
    看这个: TA = class
       class procedure  f1;
       class function f2: string;
       end;
      Tb=class (Ta)
        end;class procedure TA.f1;
    begin
    showmessage(f2);
    end;class function TA.f2: string;
    begin
    Result:=Self.ClassName;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    ta.f1;  //ta
    tb.f1;  //tb
    end;
      

  16.   

    哦,还有调用TA.f1时得到的结果并不等于(PInteger(integer(t1))^),后者是类的VMT———————————————————————————————————————我这里相等!
      

  17.   

    一些类方法,它们对类进行操作,而不是对具体的对象进行操作。
    在定义类方法的时候,使用保留字class对过程或函数进行说明。在定义类方法的时候,标识符Self将代表类方法被调用的类。不可以使用Self访问类的字段、属性和普通方法,但是可以通过Self调用构造函数和其他类方法。
      

  18.   

    该问题该完了吧
    Eastunfail(恶鱼杀手) 和 阿德都说完了,
    现在只有学习的份了
      

  19.   

    在函数的前面加上Class表示这个函数不用该类实例化就可以调用
    比如说:
    type
      A = Class(TObject)
      private
        class function Func():Res;
      protet
      public
    函数Func不需要A类的实例就可以调用
    调用方法为: A.Func;
      

  20.   

    TO myling(阿德) 
    我所说的"调用TA.f1时得到的结果并不等于(PInteger(integer(t1))^),后者是类的VMT"有误,经过验证后,两者是相等的。谢谢纠正。
      

  21.   

    呵呵还没结贴呀?
    zhxfzhxf1(zhxfzhxf1)互相学习:-) 
      

  22.   

    表示类函数,可以不依靠类的实例而可以引用的函数.
      最经典的代表就是CREATE 函数.
      但是在类函数中不可以引用类的一些方法属性