我有这段类似帕斯卡的代码:
const str = "three";
function foo(n : Integer) : String
const str = "two " ^ str;
function bar(n : Integer) : String
const str = "one" ^ str;
begin
if n=0 then bar := str
else bar := foo(n-1);
end;
begin
if n=0 then foo := str
else foo := bar(n-1);
end;
begin
writeln(foo(2));
writeln(foo(3));
end.
我想弄清楚如果:
- 该语言支持静态绑定
- 该语言支持动态绑定
对于第一种情况,我认为它会是:“一一二三” 对于后一种情况,实际上,我不确定。
我知道静态绑定意味着实体(在我们的例子中 - const“str”)在编译期间被绑定/定义,最后调用哪个函数并不重要,这意味着它可以通过阅读函数代码来推断。我也知道动态绑定意味着实体(即“str”)依赖于调用堆栈进行绑定。相互递归让我有点困惑。那么我应该从每种情况下得到什么输出,为什么?