package com.b; public class Ponit { private double x; private double y; private double z; public Ponit() { } public Ponit(double a, double b, double c) { x = a; y = b; z = c; } public double getX() { return x; } public void setX(double a) { x = a; } public double getY() { return y; } public void setY(double b) { y = b; } public double getZ() { return z; } public void setZ(double c) { z = c; } public void count1(Ponit s) { double m = Math.sqrt((x - s.x) * (x - s.x) + (y - s.y) * (y - s.y) + (z - s.z) * (z - s.z)); System.out.println("两点的距离:" + m); } public void count2() { double ss = Math.sqrt(x * x + y * y + z * z); System.out.println("点到原点的距离:" + ss); } public void print() { System.out.println("点的x坐标:" + x + "点的y坐标:" + y + "点的z坐标:" + z); } }
测试类:
package com.b; public class Eye { public static void main(String[] args) { Ponit p = new Ponit(1, 4, 2); Ponit p2 = new Ponit(2, 4, 1); p.count1(p2); p2.count2(); p.print(); } }
总结:难以下手的是:那个求两点间的距离,因为是三维空间,所以并不是需要把每一个坐标x,y,z都声明,有get方法,还有就是公式。自己对方法的理解还有很大缺陷。若有更好的方法,欢迎reply