允许递归构造函数可能有什么好处(如果有)? [英] What is the possible benefit (if any) of allowing recursive constructors?

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

问题描述

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



要记住的事情:




  • 构造函数的返回类型为void。由于它是void方法,因此您无法利用递归的全部功能。

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

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



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

解决方案

构造函数(彼此调用时)就像返回 void 的方法一样。因此,它们产生结果的唯一方法是产生副作用。然后,这仅限于使它们正在构造的对象发生变异,或者使作为参数传入的值发生变异。后者在构造函数中是一个很讨厌的想法。构造函数通常从其参数中获取信息而无需对其进行突变。



因此,对正在构造的对象进行突变是唯一的选择,以便能够以任何方式跟踪递归的进度,以便最终终止。与普通构造函数中的简单循环相比,很难看到它更容易编写,更清晰易读等。



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

 类节点
{
节点_left,_right;

公共节点(左节点,右节点)
{
_left = left!= null?新Node(left._left,left._right):null;
_right = right!= null吗?新Node(right._left,right._right):null;
}
}

此处是节点构造函数会自行调用,但通过新表达式。这是关键的区别。 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天全站免登陆