新手学习,求各位大神看看给个代码
有一个水果箱(box),箱里有水果(Fruit),每一种水果都有不同的颜色和重量,水果有苹果(Apple),橘子(Orange),梨(Pear)。每个苹果都有不同的颜色和重量,每个橘子都有不同的颜色和重量,每个梨都有不同的颜色和重量。可以向水果箱里添加水果(addFruit),也可以取出水果(getFruit),还可以显示水果的重量和颜色,写出实现这样方法的代码,要求实现上述功能!
求各位大神给个代码啊!谢谢!
[解决办法]
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了其他代码都不用动,程序会自动帮你识别重量,颜色以及名字的
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); } }}
[解决办法]
我这个可能不是你想要的,我只是一时兴起,回顾了一下生产者与消费者的问题。
package com.zf.test;import java.util.ArrayList;public class Test7 { public static void main(String[] args) throws Exception{ final FruitBasket basket = new FruitBasket(); final Fruit apple = new Apple("红色", 300); final Fruit pear = new Pear("黄色", 200); for(int i = 0 ; i < 10 ; i++){ //模拟 10 人取 , 10人放 new Thread(new Runnable() { //添加 @Override public void run() { int x = 0 ; while(true) try { basket.addFruit(++x % 2 == 0 ? apple : pear); } catch (Exception e) { e.printStackTrace(); } } }).start(); new Thread(new Runnable() { //取出 @Override public void run() { while(true) try { basket.getFruit(); } catch (Exception e) { e.printStackTrace(); } } }).start(); } }}//果篮class FruitBasket extends ArrayList<Fruit>{ private static final long serialVersionUID = 1L; private int max = 10 ; public FruitBasket(){}; public FruitBasket( int max ){ this.max = max ; } public synchronized void addFruit(Fruit fruit) throws Exception{ while(size() >= max){ System.out.println("篮子已经满了,请稍等!"); wait(); } add(fruit); notifyAll(); System.out.println("放入水果成功,当前水果总数:" + size()); } public synchronized Fruit getFruit() throws Exception{ while(size() <= 0){ System.out.println("篮子已经空了,请稍等!"); wait(); } Fruit fruit = remove(0); System.out.println("取出水果成功\t:" + fruit + "\t当前水果总数:" + size()); notifyAll(); return fruit ; }}//抽象父类abstract class Fruit{ protected String color ; protected double weight ; abstract void setColor(String color) ; abstract String getColor() ; abstract double getWeight(); abstract void setWeight(double weight) ; @Override public String toString() { return "颜色:" + this.color + " , 重量:" + this.weight; } }class Apple extends Fruit{ public Apple(){} public Apple(String color , double weight){ this.color = color ; this.weight = weight ; } @Override public void setColor(String color) { this.color = color ; } @Override public String getColor() { return this.color; } @Override public double getWeight() { return this.weight; } @Override public void setWeight(double weight) { this.weight = weight; } @Override public String toString() { return "苹果\t" + super.toString(); } }class Pear extends Fruit{ public Pear(){} public Pear(String color , double weight){ this.color = color ; this.weight = weight ; } @Override public void setColor(String color) { this.color = color ; } @Override public String getColor() { return this.color; } @Override public double getWeight() { return this.weight; } @Override public void setWeight(double weight) { this.weight = weight; } @Override public String toString() { return "梨子\t" + super.toString(); }}
[解决办法]
简化一点,应该为这样
//抽象父类abstract class Fruit{ private String name ; private String color ; private double weight ; public Fruit(String name){ this.name = name ; } public String getName() { return name; } 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; } @Override public String toString() { return "类型:" + this.name + ",颜色:" + this.color + " , 重量:" + this.weight; }}class Apple extends Fruit{ public Apple(){ super("苹果"); }}class Pear extends Fruit{ public Pear(){ super("梨子"); }}
[解决办法]
package fruitBean;import java.util.ArrayList;public class FruitBoxDemo { 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); } }}class Fruit{ private Integer weight; private String color; public Fruit(){ } public Fruit(Integer weight,String color){ this.color = color; this.weight = weight; } public String toString(){ StringBuffer sb = new StringBuffer(); sb.append("这个").append(this.getClass().getName()).append("的颜色是:").append(color).append(" 重量是:").append(weight); return sb.toString(); } public Integer getWeight() { return weight; } public void setWeight(Integer weight) { this.weight = weight; } public String getColor() { return color; } public void setColor(String color) { this.color = color; }}class Apple extends Fruit{ public Apple(Integer weight,String color){ super(weight,color); }}class Orange extends Fruit{ public Orange(Integer weight,String color){ super(weight,color); }}class Pear extends Fruit{ public Pear(Integer weight,String color){ super(weight,color); }}/*水果箱子*/class FruitBox{ //集合用来装水果的 private ArrayList<Fruit> fruitList = new ArrayList<Fruit>(); //用来放水果 public FruitBox addFruit(Fruit fruit){ fruitList.add(fruit); return this; } //用来取水果的集合 public ArrayList<Fruit> getFruit(){ return fruitList; }}