1. 程式人生 > >記錄一下,第一次用java寫那麼長,60行略長的程式碼,想了好久才懂怎麼用

記錄一下,第一次用java寫那麼長,60行略長的程式碼,想了好久才懂怎麼用

class Point {
    public int x;           //定義三點座標
public int y;
    public int z;
//編寫構造器
public Point (int _x, int _y, int _z) {
        x = _x;
y = _y;
z = _z;
}
    //各自設定座標的三個方法
public void setX(int _x) {
        x = _x;
}
    public void setY(int _y) {
        y = _y;
}
    public void setZ(int _z) {
        z
= _z; } //計算點到座標原點距離的方法 public void cal() { int len = x * x + y * y + z * z; System.out.println("len = " + len); } //顯示點座標 public void display() { System.out.println ("x = " + x + " y = " + y + " z = " + z); } } public class Pointf { public static void main
(String[] args) { Point a1 = new Point (1, 2, 3); a1.display(); a1.cal(); a1.setX(50); a1.display(); a1.cal(); a1.setY(50); a1.setZ(50); a1.display(); a1.cal(); } }