卡车要装载一批货物,货物有3种商品
来源:互联网 发布:苹果手机进水数据恢复 编辑:程序博客网 时间:2024/06/02 17:06
2.卡车要装载一批货物,货物有3种商品:电视、计算机和洗衣机。需要计算出大货车和小货车各自所装载的3中货物的总重量。
编写一个Java应用程序,要求有一个ComputeWeight接口,该接口中有一个方法:public double computeWeight(); 有3个实现该接口的类:Television、Computer和WashMachine。这3个类通过实现接口ComputeWeight给出自重。
有一个Car类,该类用ComputeWeight接口类型的数组作为成员,那么该数组的元素就可以存放Television对象的引用、Computer对象的引用或WashMachine对象的引用。程序能输出Car对象所装载的货物的总重量
package homework2;/*File name:Car.cppAuthor:杨柳Date:2017/11/6IDE:eclipse*/class Car {ComputeWeight[] goods;double totalWeight=0;Car (ComputeWeight[] goods){this.goods=goods;}public static void main(String[] args) {// TODO Auto-generated method stubComputeWeight[] goods=new ComputeWeight[3];goods[0]=new Television(345.6); //上转型对象goods[1]=new Computer(789.3);goods[2]=new WashMachine(576.0);Car car=new Car(goods);double totalweight;totalweight=goods[0].computeWeight()+goods[1].computeWeight()+goods[2].computeWeight();System.out.println("所装载的货物的总重量为:"+totalweight);}}interface ComputeWeight {public double computeWeight(); }public class Computer implements ComputeWeight {double cweight;Computer(double c){this.cweight=c;}public double computeWeight(){return cweight;}}public class Television implements ComputeWeight { //通过实现接口ComputerWeight给出自重double tweight;Television(double t){this.tweight=t;}public double computeWeight(){return tweight;}}public class WashMachine implements ComputeWeight{double wweight;WashMachine(double w){this.wweight=w;}public double computeWeight(){return wweight;}}
阅读全文
0 0
- 卡车要装载一批货物,货物有3种商品
- 【装载】你有一大堆货物和两条船!
- Java实例教程(3) 货物装载应用程序
- 传送带装载货物的最优解问题
- 货物管理
- 货物管理
- 运载货物
- 货物调度
- 搬货物
- 一种基于启发式算法的货物装载问题的研究
- 商品订购及货物采购信息系统(需求分析)
- 商品订购及货物采购信息系统(代码分析)
- 商店货物管理系统
- 货物的查询
- 出口货物流程
- 货物跟踪系统
- 货物出库管理信息系统
- 货物入库管理信息系统
- 丛林中的路
- jvm学习-内存分配及垃圾回收
- SSM实现简单的CRUD之Web层
- Verilog学习笔记(四)
- X-Bi-peak Number
- 卡车要装载一批货物,货物有3种商品
- 只须三步娶到白富美
- Python 模拟登录和抓取文章
- es6解构--简化代码
- 学校中有老师和学生两类人
- Spring Cloud Netflix
- MFC基于select模型的套接字类之服务器(8)
- Mybatis一对多、多对一、批量插入
- js 将canvas生成图片保存,或直接保存一张图片