需要一個 toString 方法在 TeeShirt 類別中,這個方法會返回一個字符串表示類別的狀態。這個 toString 方法通常包含類的重要屬性,例如尺寸、顏色、價格等。
假設 TeeShirt 類有 size(尺寸)、color(顏色)和 price(價格)這幾個屬性,toString 方法可能看起來如下:
java
public class TeeShirt {
private int size;
private String color;
private double price;
// Constructor and other methods are not shown for brevity
@Override
public String toString() {
return "Size: " + size + ", Color: " + color + ", Price: " + String.format("%.2f", price);
}
}
這裡的 String.format("%.2f", price) 確保價格以兩位小數顯示。請注意,實際的 toString 方法需要根據 TeeShirt 類的具體屬性來調整。