其他分享
首页 > 其他分享> > 设计一个Mypoint类,求两个点之间的距离

设计一个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;
    }
}

 

标签:distance,mypoint1,mypoint2,int,距离,Mypoint,设计,public
来源: https://www.cnblogs.com/xiexiaofei/p/11541639.html