如何在 Delphi 中获得静态(类)字段的等效项?

发布于 2024-12-24 17:17:17 字数 350 浏览 1 评论 0原文

我在一堂课上有一个手术,费用非常昂贵。 (大约需要 8 秒才能完全运行)所以,现在我决定它应该在程序开始时的“初始化”屏幕期间运行。然而,我在 Delphi 中找不到任何地方表明存在静态字段之类的东西。

我基本上需要做的是加载记录列表并让它们在程序的整个生命周期中保持活动状态。在德尔福中执行此操作的最佳方法是什么?

我会在 C# 中非常简单地执行此操作:

class Foo{
  static List<...> Bar;
}

但是在 Delphi 中,我没有看到任何用于创建静态字段的内容。我所看到的只是用于创建静态方法的 class 关键字

I have a certain operation in a class that is very expensive. (on the order of about 8 seconds to fully run) So, now I've decided it should probably run at the beginning of the program during an "initialization" screen. I can't find anywhere in Delphi indicating that there is such a thing as a static field however.

What I basically need to do is load a list of records and keep them alive throughout the life of the program. What is the best way to do this in Delphi?

I would do this in C# quite simply:

class Foo{
  static List<...> Bar;
}

However in Delphi, I'm not seeing anything for creating a static field. All I see is the class keyword for creating static methods

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过期以后 2024-12-31 17:17:17

您可以只使用全局变量。将其添加到单元的implementation 部分,使其成为该单元的本地单元。

我的 Turbo Delphi 支持 class var x:integer;,但我很确定 Delphi 7 不支持。

You can just use a global variable. Add it in the implementation section of a unit to make it local to that unit.

My Turbo Delphi supports class var x:integer;, but I'm pretty sure Delphi 7 doesn't.

耀眼的星火 2024-12-31 17:17:17

我认为现在是在 Delphi 中使用初始化终结的好时机。它位于一个单元的末尾,就在 end 之前。 您可以使用它来创建/释放这样的全局实例,或者只是设置默认变量...

unit Unit1;

interface

uses
  Classes;

function MyList: TStringList;    

implementation

var
  GMyList: TStringList;

function MyList: TStringList;
begin
  Result:= GMyList;
end;

initialization
  GMyList:= TStringList.Create;
  MyList.Append('Value 1'); 
  MyList.Append('Value 2');
  MyList.Append('Value 3');

finalization
  GMyList.Free;

end.

这只需要像这样的特殊场景。 MyList 将在您的应用程序期间保持活动状态。

请注意 GMyList 是如何在 implementation 下声明的,而 MyList 实际上是访问 GMyList 的全局函数。这是为了避免意外分配等。从任何其他单元,您可以使用 MyList 函数访问 GMyList,但无法直接访问 GMyList(这就是你想避免错误)。

I think this is a good time to use initialization and finalization in Delphi. This goes at the end of a unit, just before end. You can use it to create/free global instances like this, or just set default variables...

unit Unit1;

interface

uses
  Classes;

function MyList: TStringList;    

implementation

var
  GMyList: TStringList;

function MyList: TStringList;
begin
  Result:= GMyList;
end;

initialization
  GMyList:= TStringList.Create;
  MyList.Append('Value 1'); 
  MyList.Append('Value 2');
  MyList.Append('Value 3');

finalization
  GMyList.Free;

end.

This only calls for special scenarios like this one. MyList will be alive for the duration of your application.

Note how GMyList is declared underneath implementation and MyList is actually a global function to access GMyList. This is to avoid accidental assignments and such. From any other unit, you can access GMyList by using the MyList function, but won't be able to access GMyList directly (which is what you want to avoid bugs).

南城追梦 2024-12-31 17:17:17

如果您想要真正的东西,您将需要更新的 Delphi 版本。

然而,pascal 总是有类似的范围,但为此你应该(带着一点幻想)将一个单元视为静态最终类。

公共函数和变量在接口部分定义,私有内容在实现部分定义。
所有这些或多或少都相当于类变量和类方法。

如果您将单元命名为 loader.pas,并在接口部分定义了一个函数“init”,您甚至可以像调用类函数一样调用它:loader.init()。

初始化和终结部分相当于类构造函数和类析构函数。

我上面描述的从 Turbo plascal 到 delphi 2007 都是有意义的(我认为类变量是在 d2009 中引入的)。总而言之,我建议买一个更新的delphi。

You'll need a newer Delphi version if you want the real thing.

However, pascal has always had similar scoping, but for that you should (with a bit of fantasy) see a unit as a static final class.

Functions and variables that are public are defined in the interface section, and private stuff is defined in the implementation section.
All of those are more or less the equivalent of class variables and class methods.

If you name your unit loader.pas, and you define a function "init" in the interface section, you can even call it as if it's a class function: loader.init().

The initialization and finalization sections are the equivalent of a class constructor and a class destructor.

What i describe above makes sense from turbo plascal until delphi 2007 (i think class variables were introduced in d2009). All in all i'd advice to just get a newer delphi.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文