Java定義一個點類用來表示三維空間中的點?
class Point {
private int x,y,z;
Point(){this(0,0,0);}
Point(int ox,int oy,int oz){
setXYZ(ox,oy,oz);}
public void setX(int x){this.x=x;}
public void setY(int y){this.y=y;}
public void setZ(int z){this.z=z;}
public void setXYZ(int x,int y,int z){
setX(x);setY(y);setZ(z);}
public int getDistance(){
return x*x+y*y+z*z;
}}
public class Test{
public static void main(String[] args) {
Point p = new Point();
p.setXYZ(4,5,6);
int d = p.getDistance();
System.out.println("坐標為(4,5,6)的點到原點的距離是:"+d);
}
}