构造方法的重载

 

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 ;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值