class function或class 类函数 类过程

类函数\类过程.   它们是直接操作在类上面(没有实例化的对象)   

下面是Delphi    Help    的描述   
    
   A class method is a method (other than a constructor) that operates on classes instead of objects. The definition of a class method must begin with the reserved word 
   class. For example,   

   type   
    TFigure = class   
    public   
     class function Supports(Operation: string): Boolean; virtual;   
     class procedure GetInfo(var Info: TFigureInfo); virtual;   
     ...   
    end;   

   The defining declaration of a class method must also begin with class. For example,   

   class procedure TFigure.GetInfo(var Info: TFigureInfo);   
   begin   
    ...   
   end;   

   In the defining declaration of a class method, the identifier Self represents the class where the method is called (which could be a descendant of the class in which it is defined). If the method is called in the class C, then Self is of the type class of C. Thus you cannot use Self to access fields, properties, and normal (object) methods, but you can use it to call constructors and other class methods.   

   A class method can be called through a class reference or an object reference. When it is called through an object reference, the class of the object becomes the value of Self.


类方法(Class methods)是一类特殊的方法,它们在声明时要以 class 开头:
    type
      TFigure = class
     public
     ...
   class procedure GetInfo(var Info: TFigureInfo); virtual;
       ...
     end;
  实现时也以 class 开头:
 class procedure TFigure.GetInfo(var Info: 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; 即可直接完成调用!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蝈蝈(GuoGuo)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值