运行结果:
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/bd315c6034a85edff388d87b45540923dd547565?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
public class Pointtest {
public static void main(String[] args) {
Point point1 = new Point(0,0);
Point point2 = new Point(5,5);
Point point3 = new Point(5,5);
System.out.println(point1.equals(point2));
System.out.println(point2.equals(point3));
System.out.println(Point.distance(point1,point2));
System.out.println(Point.distance(point1,5,5));
System.out.println(Point.distance(0,0,5,5));
}
}
class Point{
private int x;
private int y;
public Point() {
super();
// TODO Auto-generated constructor stub
}
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}
public static double distance(int x, int y,int a,int b) {
return Math.sqrt((Math.pow((x-a), 2)+Math.pow((y-b), 2)));
}
public static double distance(Point p,int a,int b) {
return Math.sqrt((Math.pow((p.getX()-a), 2)+Math.pow((p.getY()-b), 2)));
}
public static double distance(Point a,Point b) {
return Math.sqrt((Math.pow((a.getX()-b.getX()), 2)+Math.pow((a.getY()-b.getY()), 2)));
}
}