gpt4 book ai didi

dart - 如何递归调用方法而不传递传递的参数?

转载 作者:行者123 更新时间:2023-12-03 04:35:18 25 4
gpt4 key购买 nike

void foo(bool b, int i, double d, String s, Object o) {
if (someCondition) {
return foo(b, i, d, s, o);
}
}
我以为存在类似
return foo(this);
在不传递参数的情况下,如何从 foo中调用 foo?有速记吗?

最佳答案

那没有。
递归调用没有什么特别的,就像其他任何调用一样,您必须传递所有参数。
您可以做的,以及我通常对递归算法所做的,就是拥有一个本地帮助器功能:

void foo(bool b, int i, double d, String s, Object o) {
void rec() {
...
if (someCondition) {
return rec();
}
}
rec();
// ...
}
该函数通常至少有一个参数,这是我真正要递归的参数,但是其余的变量不需要在每次递归调用时都传递。
另一个选择,因为这看起来像尾部递归,将被重写为 while循环:
void foo(bool b, int i, double d, String s, Object o) {
while (someCondition) {
// the fix.
}
// the actual code.
}

关于dart - 如何递归调用方法而不传递传递的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64221585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com