如何在 Delphi 中获得静态(类)字段的等效项?
我在一堂课上有一个手术,费用非常昂贵。 (大约需要 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以只使用全局变量。将其添加到单元的
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.我认为现在是在 Delphi 中使用
初始化
和终结
的好时机。它位于一个单元的末尾,就在end 之前。
您可以使用它来创建/释放这样的全局实例,或者只是设置默认变量...这只需要像这样的特殊场景。
MyList
将在您的应用程序期间保持活动状态。请注意
GMyList
是如何在implementation
下声明的,而MyList
实际上是访问GMyList
的全局函数。这是为了避免意外分配等。从任何其他单元,您可以使用MyList
函数访问GMyList
,但无法直接访问GMyList
(这就是你想避免错误)。I think this is a good time to use
initialization
andfinalization
in Delphi. This goes at the end of a unit, just beforeend.
You can use it to create/free global instances like this, or just set default variables...This only calls for special scenarios like this one.
MyList
will be alive for the duration of your application.Note how
GMyList
is declared underneathimplementation
andMyList
is actually a global function to accessGMyList
. This is to avoid accidental assignments and such. From any other unit, you can accessGMyList
by using theMyList
function, but won't be able to accessGMyList
directly (which is what you want to avoid bugs).如果您想要真正的东西,您将需要更新的 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.