1 package test_8_2; 2 3 public class Point { 4 5 /** 6 * 设计一个点类,实例化两个点后,调用方法求两点距离 7 */ 8 9 private double x; 10 private double y; 11 12 public Point() { 13 14 } 15 16 public Point(double x, double y) { 17 18 this.x = x; 19 this.y = y; 20 } 21 22 public static double getLength(Point A, Point B) { 23 24 double area; 25 26 area = Math.sqrt(Math.pow(A.x - B.x, 2) + Math.pow(A.y - B.y, 2)); 27 28 return area; 29 } 30 31 }
1 package test_8_2; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 7 Point aPoint = new Point(0, 3); 8 Point bPoint = new Point(4, 0); 9 10 double area = Point.getLength(aPoint, bPoint); 11 12 System.out.println("两点的距离为:" + area); 13 } 14 15 }
结果如下:
两点的距离为:5.0
[20-05-07][Self-test 38]Java DistanceBetweenPoints
原文:https://www.cnblogs.com/mirai3usi9/p/12842828.html