允许递归推理的可能的好处(如果有的话)是什么? [英] What is the possible benefit (if any) of allowing recursive contructors?

查看:304
本文介绍了允许递归推理的可能的好处(如果有的话)是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,构造函数不能递归。编译时错误:递归构造函数调用。让我们假设我们没有这个限制。



注意事项:




  • 构造函数的返回类型为void。因为它是一个void方法,你不能利用递归的完整权力。

  • 构造函数可以使用this()调用自身(或任何其他构造函数)。但是对此的调用必须是构造函数中的第一个语句

  • 我们可以在连续调用之间使用非本地数据,仍然可以从递归构造函数中获得一些可能的增益。



允许递归构造函数有什么好处吗?

解决方案

构造函数(当他们调用对方时)就像返回 void 的方法。因此,他们可以产生结果的唯一方法是副作用。然后,这限于突变他们正在构造的对象或通过突变作为参数传递的值。后者是一个很讨厌的想法在构造函数;一个构造函数通常从它的参数中获取信息而不改变它们。



所以改变正在构造的对象是唯一的选择,以便有任何方法跟踪递归的进度,以便最终终止。和一个简单的循环内部一个普通的构造函数相比,这很难看到如何更容易写,更清晰的阅读等。



调用另一个构造函数code> this )当然完全不同于在构造函数中使用 new 表达式:

  class Node 
{
Node _left,_right;

public Node(Node left,Node right)
{
_left = left!= null? new Node(left._left,left._right):null;
_right = right!= null? new Node(right._left,right._right):null;
}
}

code>构造函数调用自身,但通过新的表达式 。这是至关重要的区别。一个 new 表达式产生一个值,所以这是纯粹的功能,非突变的东西,并提供了一个方便的方法来做一个节点树的深复制。 / p>

In Java, constructors cannot be recursive. Compile time error: "recursive constructor invocation". Let's assume that we did not have this restriction.

Things to keep in mind:

  • The return type of a constructor is void. Since it is a void method you can't harness the complete power of recursion.
  • A constructor can invoke itself (or any other constructor) using this(). But a "call to this must be first statement in constructor"
  • We could use non local data between consecutive calls to still have some possible gain from recursive constructors.

Would there be any benefit from allowing recursive constructors?

解决方案

Constructors (when they are calling each other) are like methods that return void. Consequently the only way they can produce results is by side-effects. This is then limited to mutating the object they are constructing or by mutating the values passed in as parameters. The latter is a pretty nasty idea in a constructor; a constructor usually takes information from its parameters without mutating them.

So mutating the object being constructed is the only option in order to have any way to track the progress of the recursion, in order for it to terminate eventually. And it's very hard to see how that would be easier to write, clearer to read, etc. than a simple loop inside an ordinary constructor.

Calling another constructor (with this) from within a constructor is of course totally different from using a new expression within a constructor:

class Node
{
    Node _left, _right;

    public Node(Node left, Node right)
    {
        _left = left != null ? new Node(left._left, left._right) : null;
        _right = right != null ? new Node(right._left, right._right) : null;
    }
}

Here the Node constructor calls itself, but via a new expression. This is the crucial difference. A new expression produces a value, so this is purely "functional", non-mutating stuff, and provides a convenient way to make a deep copy of the tree of nodes.

这篇关于允许递归推理的可能的好处(如果有的话)是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆