我正在尝试在 Box 类中创建一个 toString
方法来调用 BoxTest 类。我已经设置了要调用的方法(getLength
、getHeight
、getWidth
、calculateArea
、calculateVolume
),它们本身工作得很好,但我不确定在调用 toString
时如何使用它们。
这是我当前代码的粘贴箱 ( http://pastebin.com/Ex520ST6 )。
框
public class Box
{
private double length = 1.0;
private double width = 1.0;
private double height = 1.0;
public Box(double length, double width, double height) // constructor with thrown exceptions
{
if (length <= 0)
throw new IllegalArgumentException("Values must be higher than 0");
if (width <= 0)
throw new IllegalArgumentException("Values must be higher than 0");
if (height <= 0)
throw new IllegalArgumentException("Values must be higher than 0");
this.length = length;
this.width = width;
this.height = height;
}
public void setLength(double length)
{
if (length <= 0)
throw new IllegalArgumentException("Values must be higher than 0");
this.length = length;
System.out.println("The new length is: " + length);
}
public double getLength()
{
System.out.println("The length is: " + length);
return length;
}
public void setWidth(double width)
{
if (width <= 0)
throw new IllegalArgumentException("Values must be higher than 0");
this.width = width;
System.out.println("The new width is: " + width);
}
public double getWidth()
{
System.out.println("The width is: " + width);
return width;
}
public void setHeight(double height)
{
if (height <= 0)
throw new IllegalArgumentException("Values must be higher than 0");
this.height = height;
System.out.println("The new height is: " + height);
}
public double getHeight()
{
System.out.println("The height is: " + height);
return height;
}
public double calculateArea()
{
double area = (double) (2*length*width + 2*length*height + 2*width*height);
System.out.println("The area is: " + area);
return area;
}
public double calculateVolume()
{
double volume = (double) length*width*height;
System.out.println("The volume is: " + volume);
return volume;
}
public String toString()
{
return String.format("The length is %f, the width is %f, the height is %f, the area is %f, the volume is %f,");
}
}
盒子测试
public class BoxTest
{
public static void main (String[] args)
{
Box[] boxes = new Box[4];
boxes[0] = new Box(1.0,2.0,3.0);
boxes[1] = new Box(4.0,5.0,6.0);
boxes[2] = new Box(1.0,7.0,8.0);
boxes[3] = new Box(1.0,9.0,9.0);
for (Box theBoxes : boxes)
{
System.out.printf(theBoxes.getLength(),theBoxes.getWidth(),theBoxes.getHeight(),theBoxes.calculateArea(),theBoxes.calculateVolume().toString());
}
boxes[3].setLength(11.0); // debug
}
}
- 总的来说,我走的路是对的吗
- 我应该在
toString
标题中使用"%s"
说明符 - 我在
printf
中还需要格式说明符吗?如果需要,应该是%s
还是%f
,因为我的方法的类型为double
。
谢谢!
最佳答案
toString()
重写应返回一个带有值本身的 String
,而不是依赖于 System.out.printf()
的外部使用> (该方法当然可以在类中使用,但类应该返回完全格式化的 String
,而不是包含像 %s
这样的格式化程序)。示例实现如下。
class Animal {
public String name;
public int numlegs;
public double weight;
// ...
@Override
public String toString() {
return String.format("Animal, name: %s, legs: %d, weight: %d", name, numLegs, weight);
}
}
然后,您只需调用 toString()
方法即可检索对象的完整 String
表示形式。
鼓励使用 printf()
而不是大规模的 String
连接,因为它可以使代码更清晰(至少在我看来)。
旁注:
- 您的
toString()
方法调用printf()
,但未提供应替换String
格式中的格式化程序的值。 - 对
printf()
的调用应采用String
格式作为第一个参数,并将值作为其余参数。
关于java - 如何正确调用toString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38599911/