日期:2014-05-20  浏览次数:20805 次

新手学习,求各位大神看看给个代码
有一个水果箱(box),箱里有水果(Fruit),每一种水果都有不同的颜色和重量,水果有苹果(Apple),橘子(Orange),梨(Pear)。每个苹果都有不同的颜色和重量,每个橘子都有不同的颜色和重量,每个梨都有不同的颜色和重量。可以向水果箱里添加水果(addFruit),也可以取出水果(getFruit),还可以显示水果的重量和颜色,写出实现这样方法的代码,要求实现上述功能!
  求各位大神给个代码啊!谢谢!

------解决方案--------------------
Java code

class Box{
    private Fruit fruit;
    private List<Fruit> fruitList = new ArrayList<Fruit>();
    Box(Fruit fruit){
        this.fruit = fruit;
    }
    public void  addFruit(Fruit fruit){}
    public void getFruit(Fruit fruit){}
    public String showFruit(Fruit fruit){return fruit.getColor() + fruit.getWeight();}
}
class Fruit{
    private String color;
    private double weight;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }
}

------解决方案--------------------
呵呵 楼主我改下代码,这样的话你更方便了,你如果想要添加一种水果比如桃子的话,你要改的代码很少,就只要继承Fruit类然后new一个桃子就OK了其他代码都不用动,程序会自动帮你识别重量,颜色以及名字的
Java code


package com.djk.celue;

import java.lang.reflect.Field;
import java.util.ArrayList;

/**
 * 水果接口
 * @author Administrator
 *
 */
 abstract class Fruit
 {
     
     
    private StringBuffer sb = new StringBuffer();
    public String toString()
    {
        //水果的重量
        String weigth  = null;
        
        //水果的颜色
        String color = null;
        
        //水果的全称包括包名字
        String name =null;
        
        //水果的英语名字
        String fruitname = null;
        try {
            name = this.getClass().getName();
            
            //如果name包括包名的话
            if(name.lastIndexOf(".")!=-1)
            {                
                fruitname = name.substring(name.lastIndexOf(".")+1,name.length());
            }
            else
            {
                fruitname = name;
            }
            
            //利用反射拿到fruit的私有属性
            Field  field1 = this.getClass().getDeclaredField("weight");
            field1.setAccessible(true);
            Field  field2 = this.getClass().getDeclaredField("color");
            field2.setAccessible(true);
            
            //得到重量
             weigth = (String)field1.get(this);
             
             //得到颜色
             color = (String)field2.get(this);
        } 
        catch (Exception e)
        {    
            System.out.println("error");
        }
        
        return sb.append("这个").append(fruitname).append("的颜色是").append(color).append("重量是:").append(weigth).toString();
    }
 }

 /**
  * 苹果
  * @author Administrator
  *
  */
 class Apple extends  Fruit
 {
     /**
      * 重量
      */
     private String weight;
     
     /**
      * 颜色
      */
     private String color;

     public Apple(String weight,String color)
     {
         this.weight = weight;
         this.color = color;
     }
     
    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
    
 }
 
 /**
  * 橘子
  * @author Administrator
  *
  */
 class Orange extends  Fruit
 {
     /**
      * 重量
      */
     private String weight;
     
     /**
      * 颜色
      */
     private String color;

     public Orange(String weight,String color)
     {
         this.weight = weight;
         this.color = color;
     }
     
    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
 }
 
 
 /**
  * 梨
  * @author Administrator
  *
  */
 class Pear extends  Fruit
 {
     /**
      * 重量
      */
     private String weight;
     
     /**
      * 颜色
      */
     private String color;

     public Pear(String weight,String color)
     {
         this.weight = weight;
         this.color = color;
     }
     
    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
 }
 
 /**
  * 水果箱子
  * @author Administrator
  *
  */
class FruitBox
{
    //集合用来装水果的 
    private ArrayList<Fruit> fruitList = new ArrayList<Fruit>();
    
    //用来放水果
    public FruitBox addFruit(Fruit fruit)
    {
        
     return     fruitList.add(fruit)?this:null;
    }

    //用来取水果的集合
    public ArrayList<Fruit> getFruit()
    {
        if(fruitList.isEmpty())
        {
            return null;
        }
        return fruitList;
    }
    
}

/**
 *测试类 
 * @author Administrator
 *
 */
public class FruitTest 
{
    public static void main(String[] args)
    {
        //水果箱子
        FruitBox fruitBox = new FruitBox();
        //苹果
        Fruit a = new Apple("23", "红色");
        //苹果
        Fruit d = new Apple("29", "青色");
        //橘子
        Fruit b = new Orange("24","黄色");
        //梨
        Fruit c = new Pear("28","黄色");

        //把水果都放入箱子中
        fruitBox.addFruit(a).addFruit(d).addFruit(b).addFruit(c);
        
        //把箱子中的水果拿出来 
        ArrayList<Fruit> fruitList = fruitBox.getFruit();
        for(Fruit fruit :fruitList)
        {
            System.out.println(fruit);
        }
        
    }
}