【发布时间】:2012-04-21 03:03:35
【问题描述】:
我试图了解两种同名方法之间的区别。这是我试图理解的代码...
public class Test {
public static void main(String[] args) {
MyPoint p1 = new MyPoint();
MyPoint p2 = new MyPoint(10, 30.5);
System.out.println(p1.distance(p2));
System.out.println(MyPoint.distance(p1, p2));
}
}
class MyPoint {
.....
}
public double distance(MyPoint secondPoint) {
return distance(this, secondPoint);
}
public static double distance(MyPoint p1, MyPoint p2) {
return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
有人能解释一下这两种distance() 方法之间的区别吗? MyPoint 类型实际上是什么意思?为什么其中 1 个方法有一个 MyPoint 对象,而另一个方法有 2 个 MyPoint 对象?
【问题讨论】:
标签: java types methods argument-passing