构造函数调用自身 [英] Constructor calling itself

查看:125
本文介绍了构造函数调用自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现,无参数的构造函数和多参数的构造函数cannnot称呼对方轮流。这是什么限制的根本原因?也许有人会说构造函数是在资源被初始化。因此,他们不能递归调用。我想知道,如果这是唯一的原因还是不行。函数/方法/步骤可以递归调用。为什么不构造?

I have recently found out that no argument constructor and multiple argument constructor cannnot call each other in turns. What is the underlying reason of this limitation? Some might say that constructors are where resources are initialised. So they must not be called recursively. I want to know if this is the only reason or not. Functions/methods/procedures can be called recursively. Why not constructors?

推荐答案

答案在于一个事实,即调用另一个构造是所有构造函数的第一行,因此你如果永远不会被执行状态打出来的递归因此堆栈溢出。

The answer lies in the fact that the call to another constructor is the first line of any constructor and hence your if condition to break out of recursion will never be executed and hence stack overflow.

这篇关于构造函数调用自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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