HX_*_*ned 3 delphi access-specifier subroutine
我知道 - 子程序的私有访问模式是其父代程序/过程,对吧?
有没有办法从"外部世界" - dpr或单位中的其他功能/程序访问它们?
另外 - 哪种方式需要更多的计算和空间来编译文件?
例如:
function blablabla(parameter : tparameter) : abcde;
procedure xyz(par_ : tpar_);
begin
// ...
end;
begin
// ...
end;
procedure albalbalb(param : tparam) : www;
begin
xyz(par_ : tpar_); // is there any way to make this function public / published to access it therefore enabling to call it this way?
end;
// all text is random.
// also, is there way to call it from DPR in this manner?
// in C++ this can be done by specifing access mode and/or using "Friend" class .. but in DELPHI?
Run Code Online (Sandbox Code Playgroud)
小智 7
嵌套过程/函数 - 在另一个过程或函数中声明的过程是一种特殊类型,因为它们可以访问它们嵌套的过程的堆栈(从而参数/局部变量).因此,和Delphi范围规则一样,那里无法在"父"程序之外访问它们.只有在需要利用其特殊功能时才使用它们.AFAIK Delphi/Pascal是少数拥有此功能的语言之一.从编译器的角度来看,调用有一些额外的代码来允许访问父堆栈帧IIRC.C++中的AFAIK"朋友"类/函数是不同的 - 它们是类访问方法,而在您的示例中,您使用的是普通过程/函数.在Delphi中,在同一单元中声明的所有过程/类都自动为"朋友",声明在最新的Delphi版本中使用.例如,这段代码片段可以正常工作,只要所有内容都在同一个单元中:
type
TExample = class
private
procedure HelloWorld;
public
...
end;
implementation
function DoSomething(AExample: TExample);
begin
// Calling a private method here works
AExample.HelloWordl;
end;
Run Code Online (Sandbox Code Playgroud)
注意:嵌入式例程<>私有/受保护的方法.
嵌入式例程即例程中的例程不能被外部例程访问.你已经发布了一个嵌入式例程的例子,我也听说过它们叫做Internal Routines.
这是另一个例子:
procedure DoThis;
function DoThat : Boolean;
begin
// This Routine is embedded or internal routine.
end;
begin
// DoThat() can only be accessed from here no other place.
end;
Run Code Online (Sandbox Code Playgroud)
无论可见性如何,都可以通过RTTI使用Delphi 2010调用类上的方法.我在本文中详细介绍了如何执行此操作.
如果您在同一个单元中,则无论可见性如何,都可以通过任何其他代码访问类上的方法,除非它们标记为严格私有. 本问题在接受的答案中有更多细节和良好的示例代码.
如果您使用两个不同的单元,则可以使用受保护的方法Hack来访问受保护的方法.这篇文章详细介绍了详细内容.
归档时间: |
|
查看次数: |
1950 次 |
最近记录: |