- /**
- *定义一个三维坐标的点
- *能够修改点的三维坐标
- *能够测量该点到任意一点的距离
- **/
- class Point{
- double x, y, z;
- Point(double _x, double _y, double _z){ //构造方法定义三维坐标
- x = _x;
- y = _y;
- z = _z;
- }
- void setX(double a) { //修改x坐标
- x = a;
- }
- void setY(double b) { //修改y坐标
- y = b;
- }
- void setZ(double c) { //修改z坐标
- z = c;
- }
- double getDistance(Point p) { //取一点返回任意点到此点的距离
- return (x - p.x)*(x - p.x) + (y - p.y)*(y - p.y) + (z - p.z)*(z - p.z);
- }
- }
- public class TestPoint {
- public static void main(String[] args) {
- Point p = new Point(1.0,1.0,1.0);
- Point p1 = new Point(0.0,0.0,0.0);
- System.out.println(p.getDistance(p1)); //打印出3.0
- p.setX(2.0);
- p.setY(2.0);
- p.setZ(2.0);
- System.out.println(p.x + "-" + p.y + "-" + p.z); //打印出2.0-2.0-2.0
- System.out.println(p.x + p.y + p.z); //打印出6.0
- System.out.println(p.getDistance(new Point(2.0,2.0,3.0))); //打印出1.0
- }
- }