能通过编译,但是不能运行!请各位高人帮忙看看!!
package com.wish.interfacedemo;
interface ComputerTotalSales
{
public double totalSalesByYear();
}
class Television implements ComputerTotalSales
{
public double totalSalesByYear()
{
return 200;
}
}
class Computer implements ComputerTotalSales
{
public double totalSalesByYear()
{
return 300;
}
}
class Mobile implements ComputerTotalSales
{
public double totalSalesByYear()
{
return 800;
}
}
class Shop
{
ComputerTotalSales[] goods;
double totalSales=0;
public Shop(ComputerTotalSales[] goods)
{
this.goods=goods;
}
public double giveTotalSales()
{
totalSales=0;
for(int i=0;i<goods.length;i++)
{
totalSales=totalSales+goods[i].totalSalesByYear();
}
return totalSales;
}
}
public class HappySales
{
public static void main(String[] args)
{
ComputerTotalSales[] goods=new ComputerTotalSales[10];
for(int i=0;i<goods.length;i++)
{
if(i%3==0)
{
goods[i]=new Television();
}
else if(i%3==1)
{
goods[i]=new Computer();
}
else if(i%3==2)
{
goods[i]=new Mobile();
}
Shop shop=new Shop(goods);
System.out.println("商店年度销售额:"+shop.giveTotalSales());
}
}
}
[此贴子已经被作者于2007-6-9 11:16:26编辑过]
----------------解决方案--------------------------------------------------------
public class HappySales
{
public static void main(String[] args)
{
ComputerTotalSales[] goods=new ComputerTotalSales[10];
for(int i=0;i<goods.length;i++)
{
if(i%3==0)
{
goods[i]=new Television();
}
else if(i%3==1)
{
goods[i]=new Computer();
}
else if(i%3==2)
{
goods[i]=new Mobile();
}
Shop shop=new Shop(goods); //这两句应该放在for循环外面.
System.out.println("商店年度销售额:"+shop.giveTotalSales());
}
}
}
----------------解决方案--------------------------------------------------------
谢谢!
----------------解决方案--------------------------------------------------------