如何在Delphi中使用Unit文件

发布于 2024-07-16 11:55:52 字数 610 浏览 9 评论 0原文

我只是想掌握单独单元的窍门,以使我的代码更加封装。 我正在尝试整理我的方法的公共/私有声明,以便我可以从使用 testunit 的其他单元调用它们。 在此示例中,我希望将 hellofromotherunit 设为公共,但将 stickletters 设为私有。

unit testunit;    

interface

uses
  Windows, Messages, Dialogs;    

implementation

function stickletters(a,b:string):string;
begin
  result:=a+b;
end;

procedure hellofromotherunit();
begin
 showmessage(stickletters('h','i'));
end;

end.

我似乎无法从其他单位复制私人/公共结构,如下所示:

Type
private
function stickletters(a,b:inter):integer;
public
procedure hellofromotherunit();
end

I'm just trying to get the hang of separate units to make my code more encapsulated.
I'm trying to get the public/private declarations of my methods sorted out, so I can call them from other units that use testunit. In this example I want to make hellofromotherunit public, but stickletters private.

unit testunit;    

interface

uses
  Windows, Messages, Dialogs;    

implementation

function stickletters(a,b:string):string;
begin
  result:=a+b;
end;

procedure hellofromotherunit();
begin
 showmessage(stickletters('h','i'));
end;

end.

I could not seem to copy the private/public structure from other units as in:

Type
private
function stickletters(a,b:inter):integer;
public
procedure hellofromotherunit();
end

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

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

发布评论

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

评论(4

烂柯人 2024-07-23 11:55:52

Unit 结构看起来有点像对象的公共/私有部分,你可以说它是它们的先驱。 但语法不同。

您只需在接口部分声明方法头,如下所示:

interface
  procedure hellofromotherunit();

implementation
  procedure hellofromotherunit(); begin .. end;

每个部分仅允许一个。

The Unit structure looks a bit like the public/private sections from objects, you could say it is their forerunner. But the syntax is different.

You only have to declare the method header in the interface section, as in:

interface
  procedure hellofromotherunit();

implementation
  procedure hellofromotherunit(); begin .. end;

Only one of each section allowed.

ι不睡觉的鱼゛ 2024-07-23 11:55:52

私人& 公共仅适用于班级。

您要做的是将 hellofromotherunit 声明的副本放入接口部分。 但是,请勿将粘性声明的副本放在那里。

界面部分中出现的任何内容实际上都是公开的。 仅在实现中的任何内容都是私有的。

Private & Public only apply to classes.

What you want to do is put a copy of the declaration of hellofromotherunit into the interface section. Do not put a copy of stickletter's declaration up there, however.

Anything that appears in the interface section is effectively public. Anything that's only down in the implementation is private.

慕巷 2024-07-23 11:55:52

此外,

每个单元都有两个不同的部分。 接口和实现。

接口部分包含所有公共定义(类型、过程标题、常量)。 实施部分包含所有实施细节。

当您使用一个单位时,(使用uses子句)您可以访问该单位的公共定义。 此访问不是递归的,因此如果单元 A 接口使用单元 B,并且单​​元 C 使用单元 A,则您无法访问单元 B,除非显式使用它。

实现部分可以访问接口以及两个 use 子句(接口和实现)中使用的单元。

首先编译所使用单元的接口,然后再继续编译其余单元。 这样做的好处是,您可以在实现中具有循环依赖关系:

unit A;
interface
uses B;

unit B;
interface
implementation
uses A;

编译:

  • 尝试接口 A,失败,需要 B
  • 尝试接口 B,好的!
  • 尝试一下A接口,可以!
  • 尝试实施A,好的!
  • 尝试实施B,好吧!

每个单元还有一个初始化部分(如果它有一个初始化部分,它也可以有一个终结部分。)初始化部分用于初始化单元的变量。 最终确定部分用于清理。
当您使用这些单元时,明智的做法是不要依赖其他单元的初始化。 只要保持简单和简短即可。

单元也是命名空间。
请考虑以下情况:

unit A;
interface
const foo = 1;

unit B;
interface
const foo = 2;

unit C;
interface
uses A, B;

const
  f1 = foo;
  f2 = A.foo;
  f3 = B.foo;

如果在多个使用单元中定义了标识符,则采用使用列表中可能的最后一个单元。 所以f1 = 2。但是你可以在它前面加上单元(命名空间)名称的前缀来解决这个问题。

随着 .net 的引入,允许使用多部分命名空间,这会引入其他好问题:

unit foo;
interface
type
  rec1 = record
    baz : Boolean;
  end;
var
  bar : rec1;

unit foo.bar;
interface
var
  baz : Integer;

uses
  foo, foo.bar;    
begin
  foo.bar.baz := true;
  foo.bar.baz := 1;
end.  

// 1. Which these lines gives an error and why?
// 2. Does the result change if you write uses foo.bar, foo?

在这种情况下,您会遇到冲突。 但这可以通过给予命名空间名称更高的优先级来解决。 所以第一行失败了。

In addition,

Each unit has two distinct parts. The interface and the implementation.

The interface section contains all public definitions (types, procedure headings, constants). The implementation section contains all implementation details.

When you use a unit, (using the uses clause) you get access to the public definitions of that unit. This access is not recursive, so if unit A interface uses unit B, and unit C uses unit A, you do not get access to unit B unless you use it explicitly.

The implementation section has access to the interface, to the unit used in both uses clauses (interface and implementation).

The interfaces of used units are compiled first before it continues compiling the rest. This has the advantage that you can have circular dependencies from within the implementation:

unit A;
interface
uses B;

unit B;
interface
implementation
uses A;

Which compiles:

  • try interface A, fail need B
  • try interface B, ok!
  • try interface A, ok!
  • try implementation A, ok!
  • try implementation B, ok!

Each unit also has an initialization section (and if it has an initialization section, it could also have a finalization section.) The initialization section is used to initialize the variables of the unit. The finalization sections are used to cleanup.
When you use these, its wise not to count on initializations of other units. Just keep them simple and short.

Unit also are namespaces.
Considder the following:

unit A;
interface
const foo = 1;

unit B;
interface
const foo = 2;

unit C;
interface
uses A, B;

const
  f1 = foo;
  f2 = A.foo;
  f3 = B.foo;

If an identifier is defined in multiple used units, the last unit possible in the uses list is taken. So f1 = 2. But you can prefix it with the unit (namespace) name to solve this problem.

With the introduction of .net, multi part namespaces are allowed which introduces other nice problems:

unit foo;
interface
type
  rec1 = record
    baz : Boolean;
  end;
var
  bar : rec1;

unit foo.bar;
interface
var
  baz : Integer;

uses
  foo, foo.bar;    
begin
  foo.bar.baz := true;
  foo.bar.baz := 1;
end.  

// 1. Which these lines gives an error and why?
// 2. Does the result change if you write uses foo.bar, foo?

In this case you have a conflict. But that is resolved by giving namespace names higher priority. So the first line fails.

好倦 2024-07-23 11:55:52

只需不在接口部分声明方法即可,它将保持私有。

unit Unit2;

interface
  function MyPublicFunction():Boolean;

implementation

function MyPrivateFunction():Boolean;
begin
  // blah blah
end;

function MyPublicFunction():Boolean;
begin
  // blah blah
end;
end.

Simply do not declare method in interface section and it will be kept private.

unit Unit2;

interface
  function MyPublicFunction():Boolean;

implementation

function MyPrivateFunction():Boolean;
begin
  // blah blah
end;

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