题目、编写一个名为“Box”的盒子类,要求如下:
(1)定义四个int型成员变量length、width、height和density,分别用来表示盒子的长、宽、高和密度;
(2)定义Box类的四个构造方法,来完成变量初始化工作:
第一个构造方法:public Box()
第二个构造方法:public Box(int length,int width,int height)
第三个构造方法:public Box(int length,int width,int height,int density)
第四个构造方法:public Box(Box b)
(3)完善四个构造方法的方法体,灵活使用this关键字;
(4)在Box类的main方法中实例化四个Box类的对象b1、b2、b3和b4,分别调用四个构造方法来完成变量初始化工作,最后分别输出对象b1、b2、b3和b4的各自变量值。
前三个构造方法我都懂,但是第四个构造方法是什么意思呢?求高手解答,谢谢了
以下是我编的代码public class Box {
public int length=10;
int width=10;
int height=10;
int density=10;
public Box(){
length=10;
        width=15;
        height=12;
        density=8;            
}
public Box(int length,int width,int height){
this.length=length;
this.width=width;
this.height=height;
}
public Box(int length,int width,int height,int density){
this.length=length;
this.width=width;
this.height=height;
this.density=density;
}
public Box(Box b){

}
public void Say(){
System.out.print("长是:"+length);
        System.out.print("宽是:"+width);
        System.out.print("高是:"+height);
        System.out.println("密度是:"+density);
        System.out.print("体积是:"+length*width*height);
        System.out.println("重量是:"+length*width*height*density);
        System.out.println("===============================");
}
public static void main(String[] args) {
Box b1 =new Box();
Box b2 =new Box(25,25,25);
Box b3 =new Box(30,40,50,4);
b1.Say();
b2.Say();
b3.Say();
}}

解决方案 »

  1.   

    Box b4 = new Box(new Box());
    b4.Say();
    跟前三个没什么区别,只是个对象跟你传String是一样的
    public Box(String str){
    System.out.println(str);
    }
      

  2.   


    定义Box类的四个构造方法,来完成变量初始化工作: public Box(Box b) {
    this.length = b.length;
    this.width = b.width;
    this.height = b.height;
    this.density = b.density;
    }