Java构造函数 [英] java constructor
本文介绍了Java构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么Java不支持c ++中的默认参数构造函数?
why java does not support default argument constructors as in c++?
推荐答案
以便您有理由使用C ++?
So that you can have a reason to use C++?
有点麻烦,但是您可以通过多个构造函数实现相同的效果:
C ++
It''s a bit more long-winded, but you can achieve the same effect with multiple constructors:
C++
thing(int required, int optional = 3) {
field1 = required;
field2 = optional;
}
Java
Java
thing(int required, int optional) {
field1 = required;
field2 = optional;
}
thing(int required) {
this(required, 3);
}
[第二种形式也适用于C ++!]
彼得
投票,并根据需要将其标记为接受.
[This second form works in C++ too!]
Peter
Vote for answers, and mark them accepted if you like them.
这篇关于Java构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文