この質問にどのように対処すればよいかわかりません。これが私のコードです:
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);
どうすればいいですか?