【发布时间】:2013-06-02 23:41:12
【问题描述】:
我目前遇到问题,找不到方法。问题来了……
完成下面的 Point 类:
public class Point{
private int x;
private int y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
}
这样下面的代码就会产生如下输出:
public class TestClass{
public static void testEqual(Point p1, Point p2){
if (p1.equals(p2)){
System.out.println("The two points are equal\n"+p1);
}else{
System.out.println("The two points are not equal");
System.out.println("First Point: "+ p1);
System.out.println("Second Point: " + p2);
}
}
public static void main(String [] args){
Point p1 = new Point(2,3);
Point p2 = Point.clonePoint(p1);
Point p3 = new Point(1,1);
Point p4 = new Point(2,3);
testEqual(p1,p2);
testEqual(p1,p3);
testEqual(p1,p4);
testEqual(p2,p4);
}
}
输出
The two points are equal
The X Coordinate is 2 and the Y Coordinate is 3
The two points are not equal
First Point: The X Coordinate is 2 and the Y Coordinate is 3
Second Point: The X Coordinate is 1 and the Y Coordinate is 1
The two points are equal
The X Coordinate is 2 and the Y Coordinate is 3
The two points are equal
The X Coordinate is 2 and the Y Coordinate is 3
除了这行Point p2 = Point.clonePoint(p1);,我什么都能理解
我该如何解决?
【问题讨论】:
-
您正在查看静态方法,而不是构造函数。
-
return new Point(p.x, p.y); -
解决什么? Point 类在级别类中有一个静态方法,该方法返回一个 Point
-
@nachokk "完成下面的课程"
标签: java object constructor