设计一个Mypoint类,求两个点之间的距离

package Test;

public class test6 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Mypoint mypoint1 = new Mypoint(1,1);
        Mypoint mypoint2 = new Mypoint(2,2);
        double distance = Mypoint.getDistance(mypoint1, mypoint2);
        System.out.println("这两个点的距离是:"+distance);
    }

}
class Mypoint {
    
    private int x = 0;
    private int y = 0;
    Mypoint(){
        x = 0;
        y = 0;
    }
    Mypoint(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 static double getDistance(Mypoint mypoint1,Mypoint mypoint2) {
        
        double distance = Math.sqrt(Math.pow((mypoint1.getX()-mypoint2.getX()), 2)+Math.pow((mypoint1.getY()-mypoint2.getY()), 2));
        return distance;
    }
}

 

上一篇:第6次作业--static关键字、对象


下一篇:编写一个表示二维平面上的点的类MyPoint,满足以下条件: 1、定义private的成员变量x和y,表示点的x和y坐标,类型为double