有一个水果箱(box),箱子里装有水果(fruit) 每一种水果都有不同的重量和颜色,水果有:苹果,梨,桔子.每个苹果(apple)都有不同的重量和颜色,每个桔子(orange)都有不同的重量和颜色,
每个梨(pear)都有不同的重量和颜色。可以向水果箱(box)里添加水果(addfruit),也可以取出水果(getfruit),还可以显示水果的重量和颜色,
写出实现这样的方法的代码.要求实现上述功能。

解决方案 »

  1.   

    class Fruit:
    String color;
    String weight;
    class Apple extends Fruit;
    class Pear extends Fruit;
    class Orange extends Fruit;
    class Box:
    void addFruit(Fruit fruit);
    Fruit getFruit(int index);
    Fruit[] getFruit(Fruit fruit);
    ...
      

  2.   

    可以发详细的代码吗?包括main 方法的实现部分
      

  3.   

    class Fruit
    {
      Color    color;
      int      weight;
      Fruit(Color c,int w)
      {
       color=c;
       weight=w:
      }
      void     SetColor(Color c)
      {
         color=c;
      }
      Color GetColor()
      {
       return color;
      }
      void SetWeight(int w)
      {
       weight=w;
      }
      int Getweight()
      {
       return weight;
      }
    }class Apple extends Fruit
    {
      Apple(Color c,int w)
      {
       super(Color c,int w):
      }
    }class Pear extends Fruit
    {
      Pear (Color c,int w)
      {
       super(Color c,int w):
      }
    }class Orange extends Fruit
    {
      Orange (Color c,int w)
      {
       super(Color c,int w):
      }
    }class Box
    {
      ArrayList fruit=new ArrayList();
      void addFruit(Fruit f)
      {
       fruit.add(f);
      }
      Fruit getFruit(int index)
      {
      return fruit.remove(index);
      }
    }
    }
      

  4.   

    class Test
    {
      static void main(String[] args)
      {
      Apple apple=new Apple(Color.Red,50);
      Pear pear=new Pear(Color.Yelloe,60);
      Orange or=new Orange(Color.Orange,40);
      Box box=new Box();
      box.addFruit(apple);
      box.addFruit(pear);
      box.addFruit(or);
      box.getFruit(2);
      }
    }