找个高手帮我解析一下个JAVA题目! PS:不要帮我做,我要自己做

来源:百度知道 编辑:UC知道 时间:2024/05/28 06:06:30
 卡车要装载一批货物,货物由种商品:电视、计算机和洗衣机。需要计算出大货车和小货车各自所装载的3种货物的总重量。
要求有一个ComputeWeight接口,该接口中有一个方法:
public double computeWeight()
有3个实现该接口的类:Television、Computer和WashMachine.这3个类通过实现接口ComputeWeight给出自重。
有一个Car类,该类用ComputeWeight接口类型的数组作为成员,那么该数组的单元就可以存放Television对象的引用、Computer对象的引用或WashMachine对象的引用.程序能输出Car对象所装载的货物的总重量。

我想问下,其实这个题目是叫我们干什么的啊? 怎么看得迷迷糊糊的~

首先是要有一个ComputeWeight的接口,里面包括computeWeight方法;

构造Television、Computer和WashMachine三个类,分别实现ComputeWeight接口,每个类应包含自身重量的属性,computeWeight方法可设置为返回该重量属性;如:
class Television implements Compute
Weight{
private double weight;
.......
//override
public double computeWeight(){
return this.weight;
}
....
}

构造一个Car类,用ComputeWeight接口类型的数组作为成员,如
ComputeWeight[] weights = new ComputeWeight[3];
该数组的单元就可以存放Television对象的引用、Computer对象的引用或WashMachine对象的引用.如
weights[0] = new Television();

然后可以根据weights数组计算机总重量,如:
total = weights[0].computeWeight() + weights[1]......;

不知是否清楚一点点了?