好吧,我假设您没有使用Box编写class关键字,并且还省略了宽度,高度和深度声明以保存输入。
在Java中,如果您不提供任何构造函数,默认情况下默认构造函数不包含任何参数。所以如果你没有在类Box中写任何东西,你仍然可以在main中调用它的基本构造函数:
class Box{
}
class CallingClass{
public static void main(String args[]){
Box box = new Box(); // this would work.
}
}如果您提供其他一个构造函数,那么在您明确声明它之前,未声明的默认构造函数将不再可用。
class Box{
public Double height;
public Box(Double height){
this.height = height;
}
}
class CallingClass{
public static void main(String args[]){
Box box = new Box((double)50); // this would work.
Box anotherBox = new Box(); // this will give you an error.
}
}迅速超过建设者:
public Box(){...} // default constructor in which you allow caller to not worry about initialization.
public Box(Box boxToCopy){...} // copy constructor for creating a new box from the values of an old one.
public Box(double height, double width, double depth){...} // should create a box with specified dimensions.