简体   繁体   English

这是递归的还是不递归的?

[英]Is this recursive or not?

I have 2 methods in a class, both methods have the same purpose, but one is recursive, the other is not.我在 class 中有 2 种方法,两种方法的目的相同,但一种是递归的,另一种不是。

I want to know if the following method called nonRecursivePrintFancyTree() is recursive or not, it does not call itself back.:我想知道以下称为nonRecursivePrintFancyTree()的方法是否是递归的,它不会回调自身。:

PrintFancyTree() {
    String outputString= "";
    outputString += printFancyTree( 2*index, _prefix);
    outputString += printFancyTree( 2*index + 1, _prefix);
}

So I know PrintFancyTree is recursive, it calls itself back, now here is the 'non recursive' one:所以我知道PrintFancyTree是递归的,它会回调自己,现在这里是“非递归”的:

nonRecursivePrintFancyTree() {
    String outputString= "";
    outputString += this.printFancyTree( 2*index, _prefix);
    outputString += this.printFancyTree( 2*index + 1, _prefix);
}

Is it non recursive or not?它是非递归的吗? It calls another method from the class.它从 class 调用另一个方法。

It's not recursive as it doesn't call itself (or other method that calls the original), the fact that it does call a recursive method doesn't make the original method recursive.它不是递归的,因为它不调用自身(或调用原始方法的其他方法),它确实调用递归方法的事实不会使原始方法递归。 This has to do with abstraction, how "printFancyTree" works doesn't matter for "nonRecursivePrintFancyTree", what matters is what "printFancyTree" DOES.这与抽象有关,“printFancyTree”的工作方式与“nonRecursivePrintFancyTree”无关,重要的是“printFancyTree”的作用。

You printFancyTree is defined with no arguments.您 printFancyTree 的定义没有 arguments。 That is why it is calling other method.这就是它调用其他方法的原因。

Redefine your method with an argument and call, it will become recursive.用参数和调用重新定义你的方法,它会变成递归的。

A method that calls itself is called recursive method.调用自身的方法称为递归方法。 The method nonRecursivePrintFancyTree doesn't call itself, therefore it's not a recursive method.方法 nonRecursivePrintFancyTree 不会调用自身,因此它不是递归方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM