在Java中创建具有多个构造函数的新对象时,它基本上是按顺序排列的吗?例如,如果你有一个包含多个int的构造函数怎么办?或者如果你想跳过构造函数参数怎么办?它会执行吗?
答案 0 :(得分:6)
每个构造函数都独立于其他构造函数。构建对象的构造函数是在new
运算符之后调用的构造函数。
答案 1 :(得分:0)
which constructor to call depends upon arguments you pass, for example lets consider following class:
public class Animal
{
String name;
String type;
Boolean carnivorous;
Animal(String name)
{
this.name = name;
}
Animal(String name, String Type)
{
this.type = type;
this.name = name;
}
}
so if you pass name and type Animal(String name, String Type) will be called , or if you pass only name then Animal(String name) will be called.
Also make note that if you define your own constructor then you override no-argument default constructor, so if you need that too along with your custom constructors then define it simply as
Animal();