定义一个point,由横坐标,纵坐标组成点,这个类有初始化,计算两点间距离的方法。
- 代码举例:
import java.util.Objects;
public class Point {
private double x;
private double y;
/*初始化
@param x横坐标
@param y纵坐标
/
public void init(double x,double y){
this.x = x;
this.y = y;
}
/
计算两点之间距离
@param p
return 距离
*/
public double distance(Point p){
return Math.sqrt( Math.pow( p.x-this.x,2 )+Math.pow( p.y-this.y,2 ) );
}
@Override
public String toString() {
return String.format( "(%f),(%f)",x,y );
}
@Override
public boolean equals(Object o) {
Point point = (Point) o;
return Math.abs( this.x-point.x )+Math.abs( this.y-point.y )<1e-6;
}
}
public class pointTest {
public static void main(String[] args) {
Point p1 = new Point();
Point p2 = new Point();
p1.init( 3,4 );
p2.init( 8,9 );
double d = p1.distance( p2 );
System.out.println(d);
System.out.println(p1);
System.out.println(p1.equals( p2 ));
}
}