package lx.method;
public class OverLoadDemo1 {
public static void main(String[] args) {
Box box1=new Box(); //创建无参构造的对象
Box box2=new Box(10.2);//创建一个参数构造的对象
Box box3=new Box(10, 2,15);//创建三个参数构造的对象
Box box4=new Box(box3);//创建以对象为参数的构造对象
System.out.println(box1.volvalue());
System.out.println(box2.volvalue());
System.out.println(box3.volvalue());
System.out.println(box4.volvalue());
}
}
/**
* 定义一个盒子类,在类中重载盒子类的四个构造方法
* @author Administrator
*
*/
class Box{
double width;
double height;
double depth;
//对象为参数的构造方法
Box(Box box){
this.width=box.width;
this.height=box.height ;
this.depth =box.depth ;
}
//三个基本数据类型为参数的构造方法,将三个参数分别赋给盒子的长宽高
Box(double width,double height,double depth){
this.width=width;
this.height = height ;
this.depth =depth;
}
//无参数的构造方式,将盒子的长宽高赋默认的初始值
Box(){
this.width =-1;
this.height =-1;
this.depth =-1;
}
//一个基本数据类型为参数的构造方法,将该参数同时赋给盒子的长宽高
Box(double len){
this.width =this.height =this.depth =len;
}
//计算盒子的长、宽、高的积
double volvalue(){
return this.width * this.height * this.depth ;
}
}