如何正确调用 toString
How to correctly invoke toString
我正在尝试在 Box class 中创建一个 toString
方法来调用 BoxTest class。我已经设置了我想调用的方法(getLength
、getHeight
、getWidth
、calculateArea
、calculateVolume
),它们自己可以正常工作,但是我不确定在调用 toString
.
时如何使用它们
这是我当前代码的一个 pastebin (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()
覆盖应该 return 一个 String
与值本身,而不是依赖外部使用 System.out.printf()
(该方法当然可以在class,但是 class 应该 return 一个完全格式化的 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
连接,因为它可以使代码更清晰(至少是 IMO)。
旁注:
- 您的
toString()
方法调用 printf()
但未提供应替换 String
. 格式的格式化程序的值
- 对
printf()
的调用应将格式 String
作为第一个参数,并将值作为其余参数。
我正在尝试在 Box class 中创建一个 toString
方法来调用 BoxTest class。我已经设置了我想调用的方法(getLength
、getHeight
、getWidth
、calculateArea
、calculateVolume
),它们自己可以正常工作,但是我不确定在调用 toString
.
这是我当前代码的一个 pastebin (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()
覆盖应该 return 一个 String
与值本身,而不是依赖外部使用 System.out.printf()
(该方法当然可以在class,但是 class 应该 return 一个完全格式化的 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
连接,因为它可以使代码更清晰(至少是 IMO)。
旁注:
- 您的
toString()
方法调用printf()
但未提供应替换String
. 格式的格式化程序的值
- 对
printf()
的调用应将格式String
作为第一个参数,并将值作为其余参数。