无参数构造函数调用 2 个参数构造函数
我试图调用使 2-arg 构造函数成为默认构造函数。 我的意思是;当调用无参构造函数时,它会调用 具有默认值的 2-arg 构造函数。
public class Foo
{
int foo1;
int foo2;
public Foo()
{
Foo(0, 0); //error //I also tried this.Foo(0,0);
}
public Foo(int one, int two)
{
this.foo1 = one;
this.foo2 = two;
}
}
如何调用第二个构造函数?
I am trying to call to make a 2-arg constructor the default constructor.
By this I mean; when the no-arg constructor is called, it calls the
2-arg constructor with default values.
public class Foo
{
int foo1;
int foo2;
public Foo()
{
Foo(0, 0); //error //I also tried this.Foo(0,0);
}
public Foo(int one, int two)
{
this.foo1 = one;
this.foo2 = two;
}
}
How do I call the 2nd constructor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需写
注意它必须是构造函数中的第一件事。
(这是在§中指定的8.8.7.1《Java 语言规范,Java SE 8 版》的“显式构造函数调用”,它还指定了如何调用特定的超类构造函数。)
Just write
Note that it has to be the very first thing in the constructor.
(This is specified in §8.8.7.1 "Explicit Constructor Invocations" of The Java Language Specification, Java SE 8 Edition, which also specifies how to invoke a specific superclass constructor.)