delphi类方法_了解Delphi类方法

delphi类方法

In Delphi, a method is a procedure or function that performs an operation on an object. A class method is a method that operates on a class reference instead of an object reference.

Delphi中 ,方法是对对象执行操作的过程或函数。 类方法是对类引用而不是对象引用进行操作的方法。

If you read between the lines, you will find that class methods are accessible even when you haven't created an instance of the class (the object).

如果您在两行之间阅读,您会发现即使没有创建类的实例(对象),也可以访问类方法。

类方法与对象方法 ( Class Methods vs. Object Methods )

Every time you create a Delphi component dynamically, you use a class method: the Constructor.

每次动态创建Delphi组件时 ,都使用一个类方法: Constructor

The Create constructor is a class method, as opposed to virtually all other methods you'll encounter in Delphi programming, which are object methods. A class method is a method of the class, and appropriately enough, an object method is a method that can be called by an instance of the class. This is best illustrated by an example, with classes and objects highlighted in red for clarity:

与在Delphi编程中遇到的几乎所有其他方法(对象方法)相反,Create构造函数是一个类方法。 类方法是类的方法,而适当的话,对象方法是可以由类的实例调用的方法。 最好用一个示例来说明,为清楚起见,用红色突出显示了类和对象:


myCheckbox := TCheck

Here, the call to Create is preceded by the class name and a period ("TCheckbox."). It's a method of the class, commonly known as a constructor. This is the mechanism by which instances of a class are created. The result is an instance of the TCheckbox class. These instances are called objects. Contrast the previous line of code with the following:

在这里,对Create的调用以类名和句点(“ TCheckbox。”)开头。 这是类的方法,通常称为构造函数。 这是创建类实例的机制。 结果是TCheckbox类的实例。 这些实例称为对象。 将上一行代码与以下内容进行对比:


my

Here, the Repaint method of the TCheckbox object (inherited from TWinControl) is called. The call to Repaint is preceded by the object variable and a period ("myCheckbox.").

在此,调用了TCheckbox对象的Repaint方法(从TWinControl继承)。 对Repaint的调用在对象变量和句点(“ myCheckbox。”)之前。

Class methods can be called without an instance of the class (e.g., "TCheckbox.Create"). Class methods can also be called directly from an object (e.g., "myCheckbox.ClassName"). However object methods can only be called by an instance of a class (e.g., "myCheckbox.Repaint").

可以在没有类实例的情况下调用类方法(例如,“ TCheckbox.Create”)。 也可以直接从对象(例如,“ myCheckbox.ClassName”)中调用类方法。 但是,对象方法只能由类的实例调用(例如,“ myCheckbox.Repaint”)。

Behind the scenes, the Create constructor is allocating memory for the object (and performing any additional initialization as specified by TCheckbox or its ancestors).

在幕后,Create构造函数正在为对象分配内存 (并执行TCheckbox或其祖先指定的任何其他初始化)。

实验自己的类方法 ( Experimenting With Your Own Class Methods )

Think of AboutBox (a custom "About This Application" form). The following code uses something like:

考虑AboutBox(自定义的“关于此应用程序”表单)。 以下代码使用类似以下内容的代码:

procedure TfrMain.mnuInfoClick(Sender: TObject) ;
begin
AboutBox:=TAboutBox.Create(nil) ;
try
AboutBox.ShowModal;
finally
AboutBox.Release;
end;
end;
This, of course, is a very nice way to do the job, but just to make the code easier to read (and to manage), it would be much more efficient to change it to:
procedure TfrMain.mnuInfoClick(Sender: TObject) ;
begin
TAboutBox.ShowYourself;
end;
The above line calls the "ShowYourself" class method of the TAboutBox class. The "ShowYourself" must be marked with the keyword "
class": class ”标记:
class procedure TAboutBox.ShowYourself;
begin
AboutBox:= TAboutBox.Create(nil) ;
try
AboutBox.ShowModal;
finally
AboutBox.Release;
end;
end;

Things to Keep in Mind

  • The definition of a class method must include the reserved word class before the procedure or function keyword that starts the definition.

    在启动定义的过程或函数关键字之前,类方法的定义必须包括保留字类。
  • AboutBox form is not auto-created (Project-Options).

    AboutBox表单不是自动创建的(Project-Options)。
  • Put AboutBox unit to the uses clause of the main form.

    将AboutBox单元放入主窗体的uses子句中。
  • Don't forget to declare the procedure in the interface (public) part of the AboutBox unit.

    不要忘记在AboutBox单元的接口(公共)部分中声明该过程。

翻译自: https://www.thoughtco.com/understanding-class-methods-1058182

delphi类方法

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值