我不确定如何解决这个问题。这是我的代码:
public interface Stuff {
public String description();
public double weight();
}
class Bag implements Stuff {
public String description() {
return "bag of";
}
public double weight() {
return 10.50;
}
}
class StuffWrapper implements Stuff {
Stuff item;
public StuffWrapper(Stuff item) {
this.item = item;
}
public String description() {
return item.description();
}
public double weight() {
return item.weight();
}
}
class Material extends StuffWrapper {
String material;
double weight;
public Material(Stuff item, String material, double weight) {
super(item);
this.material = material;
this.weight = weight;
}
public String description() {
return item.description() + " :" + material+ ":";
}
public double weight() {
return item.weight() + weight;
}
}
然后我有了这个:
Stuff icStuff = new Bag();
icStuff = new Material(icStuff, "leather", 10.30);
icStuff = new Material(icStuff, "plastic", 20.50);
System.out.println(icStuff.description() + Double.toString(icStuff.weight()));
输出
bag of :leather: :plastic:41.3
完成所有这些之后如果我希望icStuff不再引用它:
icStuff = new Material(icStuff, "plastic", 20.50);
我该怎么做?
答案 0 :(得分:2)
将其分配给其他东西,或者为null,或者您希望它引用的任何内容
icStuff = null;
icStuff = Somethingelse;