interface ComputeWeight{
double computeWeight();
}
class Television implements ComputeWeight{
double weight=16.6;
public double computeWeight(){
return weight;
}
}
class AirConditioner implements ComputeWeight{
double weight=40.0;
public double computeWeight(){
return weight;
}
}
class WashMachine implements ComputeWeight{
double weight=60.0;
public double computeWeight(){
return weight;
}
}
class Truck{
Collection<ComputeWeight> goods;
Truck(Collection<ComputeWeight> goods2){
this.goods=goods2;
}
public double getTotalWeight(){
double weight=0;
Object o[]=goods.toArray();
for(int i=0;i<goods.size();i++) {
if(o[i] instanceof Television) {
Television t=new Television();
weight=weight+t.computeWeight();
}
if(o[i] instanceof WashMachine) {
WashMachine b=new WashMachine();
weight=weight+b.computeWeight();
}
if(o[i] instanceof AirConditioner){
AirConditioner c=new AirConditioner();
weight=weight+c.computeWeight();
}
}
return weight;
}
}
The Trucks (函数题目)
最新推荐文章于 2024-07-20 23:59:39 发布