English 中文(简体)
Delphi newbie
原标题:Delphi newbie Questions
  • 时间:2010-09-06 09:26:10
  •  标签:
  • delphi

我有几个新的问题,我似乎无法找到答案。

Variables

我已注意到,在有些评估中,它们宣布了表格的私人或公共部分的变数,但在表格执行部分宣布这些变数的其他人中,是否有理由这样做或只是用户的选择?

Procedures / Functions

我再次指出,在有些评估中,某些程序/职能是以表格的私人/公共部分形式宣布的,然后以表格名称的形式确定。

Procedure Tform1.testproc;
Begin
   Blah
End;

而在另一些评估中,它们不是以形式宣布的,也不是以表格名称预先确定,而是有理由这样做? 还有哪一种最佳方法?

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

很多人

Colin

最佳回答

这一切都取决于可见度。

单位接口科宣布的类型、变数、常数、程序和职能(但不属于类别和其他类型定义的范围)可见于其他单位,而单位执行部分宣布的类型、变数、常数、程序和职能只能在同一单位使用(仅低于声明)。 因此,如果你需要某个单位的类型/变数/功能/......,但并不期望识别器在单位之外具有意义,那么,在执行工作科宣布这些识别器在需要之前是正确的。

此外,在states上,他们的识别特征可以宣布为私人、严格私人、公共、受保护和公布。 这又由于不同的可见度。 私人识别器只能在班级内使用(或同一单位界定的其他班级,除非strict<>/em> private),等等。

此外,注意到:

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>/code>是 > /em>TForm1的成员,这一类别的每一例,即这一类别的每一件标的(即该类别的每一种形式)都将有own<>>>>>>>>alpha/code>。 另一方面,在任何类别外的单位申报的<代码>>>为“单位1”,即每栏<>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.

页: 1 第一项提及这一职能是声明,它是<条码>内幕/代码”部分的类别声明的一部分,其他类别和单位将看到这一点。

最后,我谨建议正式文件Delphi,该文件非常好。 可查阅http://docwiki.embarcadero.com/RADStudio/en/Delphi_Language_Guide_Index

问题回答

Variables/procedures/functions

这一切都发生在信息应该去的地方。

如果该表格采用一种特定的方法或变数,则你必须以表格本身的形式予以宣布。 如果该表格适用于所有情况,那么你将表格放在实施部分,使之具有全球性质——它不属于此类内容。

在很多情况下,你获得的只是一种单一形式的例子,因此,就功能而言,你通常在什么地方这样做,但认为良好做法是,把你可以采取的形式,而不是执行部分。 这是因为它保留了与形式本身有关的信息,而不是依靠基本上可以从任何地方获得的全球信息。

Uses

有两个不同的地方可以提出使用条款,而你们都注意到这两个条款。

之所以需要,是因为Delphi汇编者是作为单一工具汇编者实施的。 这使情况迅速,但这也意味着你必须提前宣布所有事项,因此,它知道预期会是什么。

在使用<条码>后使用条款时,该单位在申报所有类型之后才能阅读。 这使你能够有units refer to each other, i.e. Unit1 refer to Unit2 and Unit2 refer to Unit1,因为所有要求的定义将在引入对其他定义的依赖之前提供。 如果两者都在其“最高”使用条款中相互提及,则是不可能的。

既然你可以肯定,世界基督教协进会的单位没有提及你在法典中拥有的任何单位,他们就能够安全地把他们列入最高位置。 对于你在项目上创建的单位,也不一定如此,因此,在另一个<代码>使用条款中添加这些单位更为安全。

它包含所有的Delphis模块系统(此处称为units)。

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.

考虑:

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;




相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...