public Foo(String name, double price)
{
this.name = name;
this.price = price;
}
// Overrride toString() for use of System.out.print() with Foo objects
public String toString()
{
return "Name: " + name + "\nPrice: $" + price + "\n";
}
System.out.println(anyFoo); 只有在小数点后有两位数字的双精度时才会以正确的格式显示价格。如果我使用 return 语句,如何像 printf 一样格式化?
谢谢!