public class MyPoint {
    private double x;
    private double y;

    public MyPoint() {
        this.x = 0;
        this.y = 0;
    }

    public MyPoint(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public double distance(MyPoint p) {
        double dx = p.getX() - x;
        double dy = p.getY() - y;
        return Math.sqrt(dx * dx + dy * dy);
    }
}

public class MyPointTest {
    public static void main(String[] args) {
        MyPoint p1 = new MyPoint(2, 3);
        MyPoint p2 = new MyPoint(10, 30.5);
        System.out.println('The distance between p1 and p2 is: ' + p1.distance(p2));
    }
}

输出结果为:

The distance between p1 and p2 is: 27.61993603726293
Java 平面坐标点类:MyPoint 实现与距离计算

原文地址: http://www.cveoy.top/t/topic/mJGF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录