Delphi新手问题

发布于 2024-09-17 20:11:49 字数 479 浏览 11 评论 0原文

我有几个 Delphi 编程新手问题,我似乎找不到答案。

变量

我注意到,在某些应用程序中,他们在表单类型的私有或公共部分中声明变量,但是在其他应用程序中,他们在表单的实现部分中声明变量,这是有原因的还是只是用户选择?

过程/函数

我再次注意到,在某些应用程序中,过程/函数是在表单类型的私有/公共部分中声明的,然后在创建时以表单名称 EG 为前缀,

Procedure Tform1.testproc;
Begin
   Blah
End;

而在其他应用程序中,它们未在表单类型中声明并且没有以表单名称为前缀,这有什么原因吗?还有哪个是最好的方法?

使用其他单元

是否有原因导致某些应用程序将通常用户创建的其他单元添加到表单实现部分之后的使用子句中,而其他应用程序将它们添加到表单单元顶部的使用子句中? 对上述问题的任何帮助/答案都会很棒

非常感

谢科林

I have a couple of newbie Delphi Programming questions that I cannot seem to find the answers to.

Variables

I have noticed that in some apps they declare variables in the private or public sections of the form type however in other apps they declare them in the implementation part of the form, is there a reason for this or is it just user choice?

Procedures / Functions

Again I have noticed that in some apps procedures / Functions are Declared in the private / public part of the form type and then when created they are prefixed by the form name EG

Procedure Tform1.testproc;
Begin
   Blah
End;

Whereas in other apps they are not declared in the form type and are not prefixed with the form name, is there a reason for this? Also which is the best way?

Using other units

Is there a reason why some apps add other units normally user created to a uses clause after the form implementation section whereas other apps add them to the uses clause @ the top of the form unit?
Any help / answers to the above questions would be great

Many thanks

Colin

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

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

发布评论

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

评论(3

橘味果▽酱 2024-09-24 20:11:49

这一切都取决于可见性。

在单元的接口部分(但在类和其他类型定义之外)声明的类型、变量、常量、过程和函数对其他单元可见,而在实现部分声明的类型、变量、常量、过程和函数一个单元的“of”只能在同一个单元中使用(并且只能在声明下方)。因此,如果您在特定单元中需要类型/变量/函数/...,但不希望标识符在单元之外有意义,那么最好在需要它们之前在实现部分中声明它们。

此外,当涉及到类时,它们的标识符可以声明为私有、严格私有、公共、受保护和发布。这又是由于不同类型的可见性造成的。私有标识符只能在类本身内部使用(或在同一单元中定义的其他类,除非严格私有),等等。

另外,请注意:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
    alpha: integer;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

var
  beta: integer;

implementation

{$R *.dfm}

end.

由于 alpha TForm1 的成员,因此该类的每​​个实例,即该表单的每个对象(也就是说,该类创建的每个表单)将有其自己 alpha 变量。另一方面,在任何类之外的单元中声明的 beta 是“每个单元一个”,也就是说,每个 TForm1 对象都会看到相同的 测试版。 (然后还有“类变量”等。请参阅文档以获取更多详细信息。)

(此外,您可能已经知道这一点,但在这种情况下,

unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm3 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.FormCreate(Sender: TObject);
begin
  beep;
end;

end.

您没有两个名为 FormCreate 的函数,但只有一个。对此函数的第一个引用是声明,它是接口部分中类声明的一部分,这是其他类和单元将看到的实际实现。 FormCreate 函数(或其定义)始终位于implementation 部分中。事实上,其他类或单元不需要。知道某个类中函数的具体实现。)

最后推荐一下Delphi官方文档,非常好。从 http://docwiki.embarcadero.com/RADStudio/en/Delphi_Language_Guide_Index 开始。

It all depends on visibility.

Types, variables, constants, procedures, and functions declared in the interface section of a unit (but outside of classes and other type definitions) are visible to other units, whereas types, variables, constants, procedures, and functions declared in the implementation section of a unit can only be used in the very same unit (and only below the declaration). Hence, if you need types/variables/functions/... in a particular unit but do not expect the identifiers to make sense outside the unit, then it is a good idea to declare them right before they are needed, in the implementation section.

Further, when it comes to classes, their identifiers can be declared as private, strict private, public, protected, and published. This is again due to different kinds of visibility. Private identifiers can only be used inside the class itself (or other classes defined in the same unit, unless strict private), and so on.

Also, notice this:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
    alpha: integer;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

var
  beta: integer;

implementation

{$R *.dfm}

end.

Since alpha is a member of the class TForm1, every instance of this class, that is, every object of this form (that is, every form created of this class) will have its own alpha variable. On the other hand, beta, being declared in the unit outside of any class, is "one per unit", that is, every TForm1 object will see the same beta. (And then there are "class variables" and such. Please see the documentation for more details.)

(Also, you probably already know this, but in a case like

unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm3 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.FormCreate(Sender: TObject);
begin
  beep;
end;

end.

you do not have two functions named FormCreate, but only one. The first reference to this function is the declaration, which is part of the class declaration in the interface section, which is what other classes and units will see. The actual implementation of the FormCreate function (or its definition) is always in the implementation section. Indeed, other classes or units do not need to know the exact implementation of the functions in a particular class.)

Finally, I would like to recommend the official Delphi documentation, which is very good. Start at http://docwiki.embarcadero.com/RADStudio/en/Delphi_Language_Guide_Index.

樱花落人离去 2024-09-24 20:11:49

变量/过程/函数

这一切都取决于信息应该去哪里。

如果它是特定于表单实例的方法或变量,那么您必须在表单类型本身中声明它。如果它适用于表单的所有实例,那么您将它放在实现部分中,这使其成为全局的 - 它不是类型的一部分。

在许多情况下,您一次不会有多个单一表单的实例,因此在这种情况下,就功能而言,将其放在哪里通常并不重要 - 但它被认为是好的做法是把你能做的事情放在表单类型中,而不是放在实现部分。这是因为它保留与表单本身相关的信息,而不是依赖于基本上可以从任何地方访问的全局信息。

Uses

有两个不同的地方可以放置uses 子句,您已经注意到它们了。

需要这样做的原因是因为 Delphi 编译器是作为单遍编译器实现的。这使得它很快,但这也意味着你必须提前声明一切,所以它知道会发生什么。

implementation 关键字之后使用 use 子句时,只有在声明所有类型之后才会读取该单元。这允许您单元相互引用,即Unit1引用Unit2 Unit2 引用 Unit1,因为在引入对另一个的依赖关系之前,所有必需的定义都将可用。如果两者都从其“顶级”使用子句中相互引用,则这是不可能的。

由于您可以确定 VCL 单元不会引用代码中的任何单元,因此可以安全地将它们包含在顶部。对于您在项目中创建的单元不一定有同样的说法,因此将它们添加到其他 uses 子句中会更安全。

Variables/procedures/functions

It all comes down to where the information is supposed to go.

If it's a method or variable specific to the instance of the form, then you have to declare it in the form type itself. If it applies to all instances of the form, then you put it in the implementation section which makes it global - it's not part of the type.

In many cases, you won't ever have more than a single instance of a single form at a time, so in that case, it generally doesn't matter where you put it, as far as the functionality is concerned - but it's considered good practice to put what you can in the form type, rather than in the implementation section. This is because it keeps the information associated with the form itself, instead of relying on global information which can basically be accessed from anywhere.

Uses

There are two different places to put the uses clause, and you've noticed both of them.

The reason this is needed is because the Delphi compiler is implemented as a single-pass compiler. This makes it fast, but it also means that you have to declare everything in advance, so it knows what to expect.

When using the uses clause after the implementation keyword, the unit is not read until after all of your types are declared. This allows you to have units refer to each other, i.e. Unit1 referring to Unit2 and Unit2 referring to Unit1, because all of the required definitions will be available before a dependency on the other is introduced. This is not possible if both refer to each other from their "top" uses clause.

Since you can be certain that the VCL units won't be referring to any of the units you have in your code, they are therefore safe to include at the top. The same cannot necessarily be said about units you create in your project, so it's safer to add them to the other uses clause.

终止放荡 2024-09-24 20:11:49

这都是关于 Delphi 的模块系统(模块在这里称为单元)。

当您在单元的接口部分声明某些内容时,它对所有其他单元都是可见的。
实施对于该单元可见。
在处理类时,private / public 仅与该特定类的成员相关。

考虑:

unit A 
//------------------------------------------------

Interface

uses ... // modules on which *iterface* depends
         // don't include here modules needed for implementation only
//------------------------------------------------

var visibleVar: Integer; // visible from other units
//------------------------------------------------

type VisibleType = class
public: 
    visibleMember: Integer; // visible from other units and classes
private:
    invisibleMember: Integer; // invisible from other units, but
                              // visible to other classes in this unit
strict private:
    reallyInvisible: Integer; // visible *only* inside this class
end;

//------------------------------------------------
//------------------------------------------------
//------------------------------------------------

Implementation

uses ... // modules on which *implementation* depends (and interface is *not*)
//------------------------------------------------

var invisibleVar: Integer; // can't reference to this from other units
//------------------------------------------------

type InvisibleType = class // can be referenced only from inside this module
public: 
    modulePrivateMember: Integer; // visible only inside module's implementation
private:
    invisibleMember: Integer; // in *this context* essentially the same as "public"
strict private:
    reallyInvisible: Integer; // same as in interface - it's just private
end;

It's all about Delphi's module system (modules are called units here).

When you declare something inside Interface part of the unit, it is visible to all other units.
Whereas Implementation is visible only for this unit.
And when dealing with classes, private / public relates only to members of this particular class.

Consider:

unit A 
//------------------------------------------------

Interface

uses ... // modules on which *iterface* depends
         // don't include here modules needed for implementation only
//------------------------------------------------

var visibleVar: Integer; // visible from other units
//------------------------------------------------

type VisibleType = class
public: 
    visibleMember: Integer; // visible from other units and classes
private:
    invisibleMember: Integer; // invisible from other units, but
                              // visible to other classes in this unit
strict private:
    reallyInvisible: Integer; // visible *only* inside this class
end;

//------------------------------------------------
//------------------------------------------------
//------------------------------------------------

Implementation

uses ... // modules on which *implementation* depends (and interface is *not*)
//------------------------------------------------

var invisibleVar: Integer; // can't reference to this from other units
//------------------------------------------------

type InvisibleType = class // can be referenced only from inside this module
public: 
    modulePrivateMember: Integer; // visible only inside module's implementation
private:
    invisibleMember: Integer; // in *this context* essentially the same as "public"
strict private:
    reallyInvisible: Integer; // same as in interface - it's just private
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文