如何在2020ideal里创建持久化类?

个人成就
获得36次点赞
内容获得16次评论
获得66次收藏
最近
文章
资源
问答
帖子
视频
课程
关注/订阅/互动
收藏
搜TA的内容 搜索 取消

delphi 集合类型常量
When Delphi invokes an event handler, the old values of local variables are wiped out. What if we want to
keep track of how many times a button has been clicked? We could have the values persist by using a unit-level variable, but it is generally a good idea to reserve unit-level
variables only for sharing information. What we need are usually called static variables or typed constants in Delphi.
当Delphi调用事件处理程序时,局部变量的旧值将被清除。 如果我们要跟踪一个按钮被单击了多少次怎么办? 我们可以通过使用单位级别的变量使值持久化,但是通常只保留单位级别的变量用于共享信息是一个好主意。
我们所需的通常在Delphi中称为静态变量或类型常量。
可变或常数 ( Variable or Constant )
Typed constants can be compared to initialized variables-variables whose values are defined on entry to their block (usually event handler). Such a variable is initialized only when
the program starts running. After that, the value of a typed constant persists between successive calls to their procedures.
可以将类型化的常量与已初始化的变量进行比较,这些变量的值在其块的入口(通常是事件处理程序)的入口处定义。 仅在程序开始运行时才初始化此类变量。 之后,在连续调用它们的过程之间,将保留类型常量的值。
Using typed constants is a very clean way of implementing automatically initialized variables. To implement these variables without typed constants, we'll need to create an
initialization section that sets the value of each initialized variable.
使用类型常量是实现自动初始化变量的一种非常干净的方法。 要在没有类型常量的情况下实现这些变量,我们需要创建一个初始化部分来设置每个初始化变量的值。
变量类型常量 ( Variable Typed Constants )
Although we declare typed constants in the const section of a procedure, it is important to remember that they are not constants. At any point in your application, if you have access
to the identifier for a typed constant you'll be able to modify its value.
尽管我们在过程的const部分中声明了类型常量,但切记它们不是常量很重要。 在您的应用程序中的任何时候,如果您可以访问类型常量的标识符,则可以修改其值。
To see typed constants at work, put a button on a blank form, and assign the following code to the OnClick event handler:
要查看工作中的键入常量,请在空白表单上放置一个按钮,然后将以下代码分配给OnClick事件处理程序:
procedure TForm1.Button1Click(Sender: TObject) ;
const
clicks : Integer = 1; //not a true constant
begin
Form1.Caption := IntToStr(clicks) ;
clicks := clicks + 1;
end;
Notice that every time you click on the button, forms caption increments steadily.Now try the following code:
请注意,每次单击该按钮时,字幕格式都会稳定增加,现在尝试以下代码:
procedure TForm1.Button1Click(Sender: TObject) ;
var
clicks : Integer;
begin
Form1.Caption := IntToStr(clicks) ;
clicks := clicks + 1;
end;
We are now using an uninitialized variable for the clicks counter. Notice that weird value in the forms caption after you click on the button.
现在,我们为点击计数器使用了未初始化的变量。 请注意,单击按钮后,表单标题中的值很奇怪。
常量类型常量 ( Constant Typed Constants )
You have to agree that idea of modifiable constants sounds a bit strange. In 32 bit versions of Delphi Borland decided to discourage their use, but support them for Delphi 1 legacy
code.
您必须同意,可修改常量的概念听起来有些奇怪。 在32位版本的Delphi Borland中,建议不要使用它们,但要为Delphi 1旧代码提供支持。
We can enable or disable Assignable typed constants on the Compiler page of the Project Options dialog box.
我们可以在“项目选项”对话框的“编译器”页面上启用或禁用可分配类型常量。
If you've disabled Assignable typed constants for a given project, when you attempt to compile previous code Delphi will give you 'Left side cannot be assigned to' error upon
compilation. You can, however, create assignable typed constant by declaring:
如果您已禁用给定项目的可分配类型常量,则当您尝试编译以前的代码时,Delphi将在编译时给您“无法将左侧分配给”错误。 但是,您可以通过声明以下内容来创建可分配的类型常量:
{$J+}
const clicks : Integer = 1;
{$J-}
Therefore, the first example code looks like:
因此,第一个示例代码如下所示:
procedure TForm1.Button1Click(Sender: TObject) ;
const
{$J+}
clicks : Integer = 1; //not a true constant
{$J-}
begin
Form1.Caption := IntToStr(clicks) ;
clicks := clicks + 1;
end;
结论 ( Conclusion )
It's up to you to decide whether you want typed constants to be assignable or not. The important thing here is that besides ideal for counters, typed constants are ideal for making
components alternately visible or invisible, or we can use them for switching between any Boolean properties. Typed constants can also be used inside TTimer's event handler to keep
track of how many times even has been triggered.
由您决定是否要键入的常量是可分配的。 这里重要的是,除了理想的计数器类型外,类型常量还非常适合使组件交替可见或不可见,或者我们可以使用它们在任何布尔属性之间进行切换。 也可以在TTimer的事件处理程序中使用类型化的常量来跟踪已触发的次数。
翻译自: https://www.thoughtco.com/understanding-typed-constants-in-delphi-1057668
delphi 集合类型常量

我要回帖

更多关于 jvm优化 的文章

 

随机推荐