我正在閱讀「Big Java Early Objects」一書。在Java中構造對象
在「Java構造對象」一節中,他們給出了下面的這個例子。
import java.awt.Rectangle;
/**
This example demonstrates constructors.
*/
public class ConstructorDemo
{
public static void main(String[] args)
{
// Constructs and prints a rectangle
System.out.println(new Rectangle(5, 10, 20, 30));
// Constructs a rectangle and saves it in a variable
Rectangle box = new Rectangle(5, 10, 20, 30);
System.out.print("box: ");
System.out.println(box);
// The constructor with no arguments
box = new Rectangle();
System.out.print("box: ");
System.out.println(box);
}
}
我認爲這段代碼應該打印出一個矩形。但是沒有任何反應,它只在控制檯打印出來:
java.awt.Rectangle[x=5,y=10,width=20,height=30]
box: java.awt.Rectangle[x=5,y=10,width=20,height=30]
box: java.awt.Rectangle[x=0,y=0,width=0,height=0]
我的Eclipse有什麼問題嗎?我該如何做這項工作?
爲什麼會打印出矩形?你打印出矩形_的意思是什麼? –
它不會繪製矩形。 – gd1
它調用'Rectangle#toString'方法,並且該字符串正在控制檯中打印。 –