【发布时间】:2023-03-23 18:46:01
【问题描述】:
我有一个问题。我第一次在 JAVA 中处理多个类。我在做这件事时遇到了一些麻烦。我创建了一个类,我将从另一个类中调用它。我想创建一个类型Coordinate,顾名思义,它包含坐标。然后,我想移动这些坐标。到目前为止,代码如下所示:
public class Coordinate {
double x;
double y;
Coordinate(){
x=0;
y=0;
}
public Coordinate(int x, int y){
this.x = x;
this.y = y;
System.out.print(x);//TO TEST WHETHER IT DOES SOMETHING
}
Coordinate shiftCoordinate(int z, int w){
this.x = x + z;
this.y = y+ w;
return new Coordinate(x,y);//ERROR: The constructor Coordinate(double, double) is undefined
}
}
它在声明的地方抛出一个错误。我不明白这个错误。在我的“主要”课程中,我做了以下事情:
void start() {
Coordinate coordinate = new Coordinate();
coordinate.x=3;
coordinate.y=4;
}
我希望这会打印3,但事实并非如此。我哪里错了?
【问题讨论】:
-
您提到的错误准确地解释了问题所在。如果要使用构造函数
Coordinate(x,y),则必须创建它。除非你自己写,否则不存在这样的构造函数。
标签: java class constructor