3. 有一个水果箱(box),箱子里装有水果(fruit)。每一种水果都有不同的重量(weight)和颜色(color),水果有:苹果(apple),梨(pear)。可以向水果箱(box)里添加水果(addFruit),也可以取出水果(getFruit)。请编写java代码实现上述功能看了很多人都说简单简单,让我很汗颜,最近准备跳,但是JAVA方面经验少之又少。
对于这道题,想拿出来探讨下水果和苹果 梨不说了,关键是这个水果箱。在这个水果箱里,保持了些什么东西?是不是加进来水果的属性都在里面?包括种类,颜色,数量。这些属性应该是累加的,而不是每次简单的加一个实例进来,取得时候也要判断有没有颜色的水果,够不够要取的分量。
那么该如何操作这样的内容呢?貌似挺复杂的。我觉得这样应该才是一个正常的水果箱。
对于这道题,想拿出来探讨下水果和苹果 梨不说了,关键是这个水果箱。在这个水果箱里,保持了些什么东西?是不是加进来水果的属性都在里面?包括种类,颜色,数量。这些属性应该是累加的,而不是每次简单的加一个实例进来,取得时候也要判断有没有颜色的水果,够不够要取的分量。
那么该如何操作这样的内容呢?貌似挺复杂的。我觉得这样应该才是一个正常的水果箱。
new Apple(0.6,"RED"),
new Pear(0.5,"YELLOW"),
new Apple(0.8,"GREEN")
}; for(Fruit f : fruits){
box.addFruit(f);
} box.toString(); box.getFruit(); box.toString();
} private static abstract class Fruit{
public double weight;
public String color;
public Fruit(double weight,String color){
this.weight = weight;
this.color = color;
} public abstract String toString();
} public static class Apple extends Fruit{ public Apple(double weight,String color){
super(weight,color);
} public String toString(){
return "apple:[weight=" + weight + ",color=" + color + "].";
}
} public static class Pear extends Fruit{ public Pear(double weight,String color){
super(weight,color);
} public String toString(){
return "pear:[weight=" + weight + ",color=" + color + "].";
}
} private static class FruitBox{ ArrayList<Fruit> list = new ArrayList<Fruit>(); public void addFruit(Fruit fruit){
list.add(fruit);
} public void addFruit(int index , Fruit fruit){
list.add(index,fruit);
} public Fruit getFruit(){
return list.remove(0);
} public Fruit getFruit(int i){
return list.remove(i);
} public String toString(){
for(Fruit f : list){
System.out.println(f);
} System.out.println("------------------------"); return null;
}
}
}
比如,如果只有重量这个属性,那么设置一个STATIC的域就可以在加水果和拿水果的时候,对这个域进行判断。用一个Map来放各种各样的水果?然后用水果种类和颜色作为组合得key,值是重量有没有人能把题给理一理不知道跟工厂模式有没有联系上面答案就是原贴里的答案,只能作为多态的一个简单例子,不能很好表达本题水果箱的意思吧。
肯定不行啊。怎么感觉水果箱在完成数据库要做的事情
就是hash吧容器是一回事,算法又是另一回事
你要做一个水果箱,那只是做一个容器而已,只要能实现就好了,List是个很好的选择
至于按特殊要求取那是算法问题,而且取水果遍历也是很正常的事情,现实中你要在一箱水果中取满足一定条件的一部分,你不需要把每只都拿出来看一下么?Map的话问题反而更多,比如复合条件,种类、颜色、重量三个属性,你说用其中两个做Key,另一个做Value,那么我要找的两个条件一个在Key里一个在Value里怎么办?还不是得老老实实的一个一个拉出来看……再如果Key中两个属性相同的水果怎么办?红色的苹果只能有一只吗?
散列是一种思想,hashMap也只是一个散列的容器,提供的是一个散列的关系。
你发现按一个key散列不方便,不会多散列几次啊?实体对象你可以把他们放在统一的list中,hash只提供散列关系。
你把一个对象放到不同的hashmap中又不会多出对象来,key按照需求使用复合主键就好了。
散列的数据结构完全可以自己来写,key相通的使用 list 来存放,不要想hashmap一样直接覆盖了。 把你想不通的地方说来,不要难在哪里都说不出来,感觉这都不能算道考编码技巧的题,无非就是考思维的清晰度。
多散列几次……空间换时间么?
散列一次只是多了一个内存地址的值而已。to zhuzeitou建议你去了解下hash的原理,很多技术论坛上都有这方面的详细解说,讲解的对象一般是hashMap 或者 currentHashMap。 散列是检索的基础,散列是一个思想,而用于计算 HashCode 的素数是数学家推到出来散列因子。 搞清楚这个也有利于划分算法和应用的界限。
public class FruitBox {
private List<Fruit> allFruits = new ArrayList<Fruit>();
public void addFruit(Fruit fruit){
allFruits.add(fruit);
}
public List<Fruit> getFruitByColor(String color){
List<Fruit> thisColorFruits = new ArrayList<Fruit>();
for (Fruit fruit:allFruits) {
if(color.equals(fruit.getColor())){
thisColorFruits.add(fruit);
}
}
return thisColorFruits;
}
public List<Fruit> getFruitByKind(String kind){
List<Fruit> thisKindFruits = new ArrayList<Fruit>();
for (Fruit fruit:allFruits) {
if(kind.equals(fruit.getName())){
thisKindFruits.add(fruit);
}
}
return thisKindFruits;
}
public List<Fruit> getFruit(String kind, String color){
List<Fruit> theFruits = new ArrayList<Fruit>();
for (Fruit fruit:allFruits) {
if(kind.equals(fruit.getName()) && color.equals(fruit.getColor())){
theFruits.add(fruit);
}
}
return theFruits;
}
}public abstract class Fruit {
private String name;
private int weigth;
private String color; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getWeigth() {
return weigth;
} public void setWeigth(int weigth) {
this.weigth = weigth;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
}}针对问题写了点代码,个人觉得按你的要求还是List比较好,
当然,如果想做个功能更强大的水果箱,就要考虑更多的问题,
由于针对水果没有什么特别,写代码时用了抽象类,这个可以根据实际修改。
其实这个问题还是有很多可以讨论的,学习
用这种方法,只要再加个结合了名字的Get方法,再加些重量上的判断,修改List里对象的重量属性,就可以实现获取指定水果的任意重量和任意颜色。但感觉很怪,水果种类不应作为一个属性,而是作为一个类,苹果和梨有自己各自的行为和属性,不能作为一个同一类而仅仅用名字区分它们。如果用List来存放这些不同水果。
用继承的方法实现水果的话,那该怎么来判断该取List哪个对象呢?去判断它们的类的类型吗?还是有别的什么方法,比如能通过苹果和梨各自实现自己的一套方法,然后通过向下转型来自己找到该掉用哪个方法来减去相应颜色的相应重量。HashMap和Hash完全不了解看书先不知道上面的这个想法是不是有点怪
反正我是想不出来,感觉很一知半解,心慌,静不下心看书了。