Delphi构造函数和类构造函数 [英] Delphi constructor and class constructor

查看:180
本文介绍了Delphi构造函数和类构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有疑问,我无法解决。我已阅读embarcadero的文档,了解类构造函数,但我无法理解的意思换句话说,构造函数类构造函数之间的使用差异是什么?我这样做:

  type 
TGeneric< T> = class
private
FValue:T;
aboutString:string;
procedure setValue(const a:T);
public
属性V:T读取FValue写入setValue;
属性about:string read aboutString;
构造函数创建;
析构函数覆盖
结束

执行

{TGeneric< T> }

构造函数TGeneric&T; .Create;
开始
继承;
aboutString:='test';
结束

相反,此代码无法正常工作:

  type 
TGeneric< T> = class
private
FValue:T;
aboutString:string;
procedure setValue(const a:T);
public
属性V:T读取FValue写入setValue;
属性about:string read aboutString;
类构造函数创建;
析构函数覆盖
结束

执行

{TGeneric< T> }

类构造函数TGeneric&T; .Create;
开始
继承;
aboutString:='test';
结束

我想答案是在文档的这一行:


通常,类构造函数用于初始化类的静态字段
或执行一种类型的初始化,这在类之前需要
或者任何类的实例可以正常工作。


告诉我如果我是正确的:




  • 构造函数:我可以使用继承的Create; ,初始化变量等等。
  • 类构造函数:当我必须立即在我的类中创建一个对象时,我可以使用它



例如看下面:

  type 
TBox = class
private
class var FList:TList< Integer> ;;
类构造函数创建;
结束

实现

类构造函数TBox.Create;
begin
{初始化静态FList成员}
FList:= TList< Integer> .Create();
结束

结束。

这里我将在调用 TBox.Create时立即创建对象在主窗体中

解决方案


  • 类构造函数执行一次当它被声明的单位被初始化时。类构造函数是一个类方法,因此 Self 指的是类而不是一个实例。

  • 一个构造函数在显式调用时执行并且具有初始化类的实例的功能。



在野外,类构造函数是罕见的,构造函数与muck一样常见。很可能你现在不需要类构造函数,所以现在可以自由地忽略它们。集中于了解如何编写和使用构造函数。



如果将来您需要初始化类所拥有的变量(而不是由实例拥有),那么您可能会发现自己想要使用类构造函数。在这个时间点之前,请随便忽略它们。


I have a doubt that I cannot solve. I have read the documentation at embarcadero for class constructors but I cannot understand the meaning of that. In other words, what is the usage difference between a constructor and a class constructor? I did this:

type
 TGeneric<T> = class
  private
   FValue: T;
   aboutString: string;
   procedure setValue(const a: T);
  public
   property V: T read FValue write setValue;
   property about: string read aboutString;
   constructor Create;
   destructor Destroy; override;
 end;

implementation

{ TGeneric<T> }

constructor TGeneric<T>.Create;
begin
  inherited;
  aboutString := 'test';
end;

Instead this code is not working properly:

type
 TGeneric<T> = class
  private
   FValue: T;
   aboutString: string;
   procedure setValue(const a: T);
  public
   property V: T read FValue write setValue;
   property about: string read aboutString;
   class constructor Create;
   destructor Destroy; override;
 end;

implementation

{ TGeneric<T> }

class constructor TGeneric<T>.Create;
begin
  inherited;
  aboutString := 'test';
end;

I guess that the answer is in this line of the documentation:

Normally, class constructors are used to initialize the static fields of the class or to perform a type of initialization, which is required before the class or any class instance can function properly.

Tell me if I am correct:

  • Constructor: I can use the inherited Create;, initialize variables and so on.
  • Class constructor: I can use this when I have to create an object in my class immediately?

For example look at below:

type
   TBox = class
   private
     class var FList: TList<Integer>;
     class constructor Create;
   end;

 implementation

 class constructor TBox.Create;
 begin
   { Initialize the static FList member }
   FList := TList<Integer>.Create();
 end;

 end.

Here I am going to create the object immediately when I call TBox.Create in the main form?

解决方案

  • A class constructor executes exactly once, when the unit in which it is declared is initialized. A class constructor is a class method, and so Self refers to the class rather than an instance.
  • A constructor executes when explicitly called and has the job of initializing an instance of a class.

In the wild, class constructors are rare, constructors are as common as muck. Quite likely you have no immediate need for class constructors so feel free to ignore them for now. Concentrate on understanding how to write and use constructors.

If in the future you ever need to initialize variables owned by the class (as opposed to owned by an instance) then you might find yourself wanting to use a class constructor. Until that point in time, do feel free to ignore them.

这篇关于Delphi构造函数和类构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆