-4

我不确定如何解决这个问题。这是我的代码:

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);

我该怎么做?

4

1 回答 1

2

将它分配给其他东西,或 null,或任何你想要它引用的东西

icStuff = null;
icStuff = Somethingelse;
于 2013-02-20T23:08:27.370 回答