我正在尝试使用 Point(double x, double y), getX(), getY()
创建一个点并使用 toString()
返回它.我无法在任何地方找到如何执行此操作的示例。
public class Point {
private final double x;
private final double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
@Override
public String toString() {
return ("(" + x + "," + y + ")");
}
}
最佳答案
您可能想这样做:
public Point(double x, double y) {
this.x = x;
this.y = y;
}
然后...
System.out.println(new Point(5.0, 5.0).toString());
我不知道为什么要在构造函数中将 this.x 和 this.y 的值设置为 1。您应该将它们设置为提供的 x 和 y 值。
您也不需要 toString()
方法中的外括号组。 return "("+ x + ","+ y + ")";
将正常工作。
关于java - 使用 Point 类的示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18951124/