建一个接口A 定义方法getList,getHeight,getColor建立Box public class Box { static HashMap al = new HashMap(); public void add(String name, A a) { al.put(name,a); } publci void remove(String name) { al.remove(name); } public A getFruit(String name) { return (A)al.get(name); }} A 是水果的接口 所有水果类都必须实现它
interface IBox { List fruits;
public void add(Fruit fruit); public Fruit remove(int index); public Color queryColor(int index); }interface Constraints { static final int APPLE = 0; static final int PEER = 1; static final int ORANGE = 2; }/** * 水果抽象类,提供水果属性的设置和获取 */ abstract class fruit { private int catagory = -1; private double weight; private Color _color;
// getter and setter method // 水果种类是不可更改的,所以该属性为只读 public void setWeight(double weight) { this.weight = wieght; }
public void setColor(Color _color) { this._color = _color; }
public int getCatagory() { return this.catagory; }
public double getWeight() { return this.weight; }
public Color getColor() { return this._color; } }class Apple extends fruit { Apple() { // 水果属性是一定的,所以在构造中进行属性赋值 super.setCatagory(Constraints.APPLE); }
public String toString() { return "这是苹果"; } }class BoxImp implements IBox { // 实现 add() remove(index)和query(index)方法即可 public Color queryColor(index) { Fruit fruit = fruits.get(index);
public class Box
{
static HashMap al = new HashMap();
public void add(String name, A a)
{
al.put(name,a);
} publci void remove(String name)
{
al.remove(name);
} public A getFruit(String name)
{
return (A)al.get(name);
}}
A 是水果的接口
所有水果类都必须实现它
{
List fruits;
public void add(Fruit fruit);
public Fruit remove(int index);
public Color queryColor(int index);
}interface Constraints
{
static final int APPLE = 0;
static final int PEER = 1;
static final int ORANGE = 2;
}/**
* 水果抽象类,提供水果属性的设置和获取
*/
abstract class fruit
{
private int catagory = -1;
private double weight;
private Color _color;
// getter and setter method
// 水果种类是不可更改的,所以该属性为只读
public void setWeight(double weight)
{
this.weight = wieght;
}
public void setColor(Color _color)
{
this._color = _color;
}
public int getCatagory()
{
return this.catagory;
}
public double getWeight()
{
return this.weight;
}
public Color getColor()
{
return this._color;
}
}class Apple extends fruit
{
Apple()
{
// 水果属性是一定的,所以在构造中进行属性赋值
super.setCatagory(Constraints.APPLE);
}
public String toString()
{
return "这是苹果";
}
}class BoxImp implements IBox
{
// 实现 add() remove(index)和query(index)方法即可
public Color queryColor(index)
{
Fruit fruit = fruits.get(index);
return fruit.getColor();
}
}
今天好好的研究了一下,感觉有点思路了,非常感谢!
{
hegiht
color
add()
remove()
}
class box implements fruit
{}