toString()
自訂 Box 類別,覆寫 toString(),可以輸出有用的資訊,而不再是 Box@1fb8ee3。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class Box { private int width; private int height; public Box(int width, int height) { this.width = width; this.height = height; } @Override public String toString() { return "寬:" + width + "px, 高:" + height + "px"; } public static void main(String[] args) { Box box = new Box(100, 50); System.out.println(box.toString()); } }
|