求两点距离,想知道自己写的那个无参构造器的对不对。。。
有参是跟着尚学堂视频学的,但是没太听懂。。。。
1.有参
public class Point {
double x,y;
int c =3;
Point(double x1,double y1){
x = x1;
y = y1;
}
public double getDistance(Point p1){
return Math.sqrt((x-p1.x)*(x-p1.x)+(y-p1.y)*(y-p1.y));
}
public static void main(String []args){
Point p1 = new Point(3.0,4.0);
Point origin = new Point(0.0,0.0);
System.out.println(p1.getDistance(origin));
}
}
2.无参
public class Point{
//属性
double x,y;
//方法
public double getDistance(double x, double y){
return Math.sqrt(x*x+y*y);
}
//入口
public static void main(String[] args){
Point p = new Point();
System.out.println(p.getDistance(3.0,4.0));
}
}