这对于 dart 来说似乎是不可能的?即让类方法递归并最终返回一个对象?当我运行递归方法时,如果它至少递归了一次,它总是返回 null...
示例:
// some class method
rock throw_rock() {
// look at its own collection of rocks
// get a rock and do a test on it
rock to_throw = this.rocks[53]; // lets assume its in a map at key 53...
if (to_throw.been_thrown == 1) {
// ok, dont throw this one, instead recurse and find another
this.throw_rock();
} else {
return to_throw;
}
}
在其他类或主类中:
rock g = rock_thower.throw_rock();
// if rock thrower has had to recurse
// g will be null...
我对 dart 非常陌生,不知道为什么会发生这种情况。有任何想法吗?这是理智的吗?
如果不是:我做错了什么?
最佳答案
需要返回 this.throw_rock()
。
也就是说,您想要返回 to_throw
变量,或者递归调用的结果。
关于javascript - dart 递归类方法总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11813501/