- 构造方法
![](https://img-blog.csdn.net/20150628143825074?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc291dGhlYXN0X3B4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20150628143836536?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc291dGhlYXN0X3B4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
- Point.java
public class Point {
private double x;
private double y;
private double z;
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
public Point(double x, double y, double z) {
super();
this.x = x;
this.y = y;
this.z = z;
}
/**
* 计算两个点的距离
* @param p
* @return
*/
public double distance(Point p){
return Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)+(z-p.z)*(z-p.z));
}
public static void main(String[] args) {
Point p1 = new Point(1, 1, 1);
Point p2 = new Point(2, 2, 2);
System.out.println(p1.distance(p2));
}
}
- Point.java
public class Point { private double x; private double y; private double z; public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } public double getZ() { return z; } public void setZ(double z) { this.z = z; } public Point(double x, double y, double z) { super(); this.x = x; this.y = y; this.z = z; } /** * 计算两个点的距离 * @param p * @return */ public double distance(Point p){ return Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)+(z-p.z)*(z-p.z)); } public static void main(String[] args) { Point p1 = new Point(1, 1, 1); Point p2 = new Point(2, 2, 2); System.out.println(p1.distance(p2)); } }