代码:
public class Point {
int x;
int y;
public void set(int xx, int yy){
x =xx;
y =yy;
}
public double calcDistance(Point p){
return Math.sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
}
public static void main(String[] args) {
Point p1 = new Point();
p1.set(3,5);
Point p2 = new Point();
p2.set(2,4);
System.out.println(p1.calcDistance(p2));
}
}
2. 代码实现交换point中两个左边的值(实现用对象作为参数)
public class Point {
int x;
int y;
public void set(int xx, int yy){
x =xx;
y =yy;
}
public double calcDistance(Point p){
return Math.sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
}
public void changePoint(Point p){
int a =p.x;
p.x = x;
x = a;
int b = p.y;
p.y = y;
y = b;
}
public void show(){
System.out.println("["+x+","+y+"]");
}
public static void main(String[] args) {
Point p1 = new Point();
p1.set(3,5);
Point p2 = new Point();
p2.set(2,4);
System.out.println(p1.calcDistance(p2));
p1.changePoint(p2);
p1.show();
p2.show();
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。