如何在Delphi中使用Unit文件

Art*_*hur 2 delphi pascal private public delphi-units

我只是试图让单独的单元挂起来使我的代码更加封装.我正在尝试将我的方法的公共/私有声明整理出来,所以我可以从其他使用的单元中调用它们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.
Run Code Online (Sandbox Code Playgroud)

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

Type
private
function stickletters(a,b:inter):integer;
public
procedure hellofromotherunit();
end
Run Code Online (Sandbox Code Playgroud)

Hen*_*man 6

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

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

interface
  procedure hellofromotherunit();

implementation
  procedure hellofromotherunit(); begin .. end;
Run Code Online (Sandbox Code Playgroud)

每个部分只允许其中一个.


Lor*_*tel 5

私人和公共仅适用于课程.

你想要做的是将hellofromotherunit声明的副本放入接口部分.但是,不要在那里贴上简报的声明副本.

界面部分中出现的任何内容都是公开的.任何仅在实施中失败的东西都是私有的.