java问题 定义一个Point点类

定义一个Point点类,成员变量有x,y,成员函数set()设置x,y的值,get()获取x,y的值,并定义一个点对象调用set()和get(),并定义一个点对象调用set()和get()构造方法重载,distance()表示2点间的距离。

实例public class Point {

private int x;

private int y;

public Point(int x, int y) {

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;

}

public double distance(Point p1,Point p2){

return Math.sqrt((p2.getX()-p2.getX())*(p2.getX()-p2.getX())+(p2.getY()-p1.getY())*(p2.getY()-p1.getY()));

}

public static void main(String args[]){

Point p1 = new Point(14,17);

Point p2 = new Point(23,90);

double s = p1.distance(p1,p2);

System.out.println("2点之间的距离为:"+s);

}

}

更多推荐

point类 java_java中point是什么意思