如何解决Java中与用户定义的方法和对象相关的以下错误?
我编写了以下Java代码:如何解决Java中与用户定义的方法和对象相关的以下错误?,java,oop,return,user-defined-functions,return-type,Java,Oop,Return,User Defined Functions,Return Type,我编写了以下Java代码: public class Point2D{ private double xc, yc; public Point2D(double x, double y){ this.xc = x; this.yc = y; } public Point2D createNewPoint(int xOff, int yOff){ this.xc = xc + xOf
public class Point2D{
private double xc, yc;
public Point2D(double x, double y){
this.xc = x;
this.yc = y;
}
public Point2D createNewPoint(int xOff, int yOff){
this.xc = xc + xOff;
this.yc = yc + yOff;
return xc;
}
public static void main(String[] args){
Point2D p1 = new Point2D(2.5, 3.5);
Point2D p3 = p1.createNewPoint(5, -2);
System.out.println(p3);
}
}
我遇到以下错误:
Point2D.java:27: error: incompatible types: double cannot be converted to Point2D
return xc;
^
1 error
有人能帮我解决这个错误,以及如何从Java中的用户定义方法返回两个变量/值吗?返回值的类型应该与返回类型匹配。您已返回类型为double的xc,但返回类型为Point2D 替换
public Point2D createNewPoint(int xOff, int yOff){
this.xc = xc + xOff;
this.yc = yc + yOff;
return xc;
}
与
另外,创建如下所示的toString实现,以便在打印Point2D对象时获得有意义的输出
返回值的类型应与返回类型匹配。您已返回类型为double的xc,但返回类型为Point2D 替换
public Point2D createNewPoint(int xOff, int yOff){
this.xc = xc + xOff;
this.yc = yc + yOff;
return xc;
}
与
另外,创建如下所示的toString实现,以便在打印Point2D对象时获得有意义的输出
为私有字段添加getter
如果需要新对象,请从createNewPoint方法返回一个新对象,如下面的解决方案所示
使用getter方法访问各个值
class Point2D {
private double xc, yc;
public Point2D(double x, double y){
this.xc = x;
this.yc = y;
}
public Point2D createNewPoint(int xOff, int yOff){
return new Point2D(this.xc + xOff, this.yc + yOff);
}
public double getXc() {
return xc;
}
public double getYc() {
return yc;
}
public static void main(String[] args){
Point2D p1 = new Point2D(2.5, 3.5);
Point2D p3 = p1.createNewPoint(5, -2);
System.out.println (p3.getXc() + ":" + p3.getYc());
}
}
为私有字段添加getter
如果需要新对象,请从createNewPoint方法返回一个新对象,如下面的解决方案所示
使用getter方法访问各个值
class Point2D {
private double xc, yc;
public Point2D(double x, double y){
this.xc = x;
this.yc = y;
}
public Point2D createNewPoint(int xOff, int yOff){
return new Point2D(this.xc + xOff, this.yc + yOff);
}
public double getXc() {
return xc;
}
public double getYc() {
return yc;
}
public static void main(String[] args){
Point2D p1 = new Point2D(2.5, 3.5);
Point2D p3 = p1.createNewPoint(5, -2);
System.out.println (p3.getXc() + ":" + p3.getYc());
}
}
如果该方法应该创建Point2D的新实例,只需使用适当的参数调用Point2D构造函数并返回它创建的对象。您现在要做的是只返回它的一个字段,x坐标。如果该方法应该创建Point2D的新实例,只需使用适当的参数调用Point2D构造函数并返回它创建的对象。你现在要做的是只返回其中一个字段,x坐标。这有一个副作用,就是修改当前的点2D,我认为这不是你想要的。应该做的是使用xc+xoff和yc+yOff作为构造函数的参数,而不将它们分配给当前的Point2D实例。我做到了!但是,它返回对象点2d的地址,我想返回参数xc的值,yc@JustAnotherDeveloper-谢谢。我同意你的评论,我也更新了答案。事实上最初我打算发布与我发布的更新相同的内容,但后来我认为这是OP在调试后可以解决的另一个问题。@MukulChikhalkar您可以通过调用相应的getters@MukulChikhalkar-您需要提供toString的实现,如答案所示。这个包与它没有任何关系,它的副作用是修改当前的Point2D,我认为这不是我们想要的。应该做的是使用xc+xoff和yc+yOff作为构造函数的参数,而不将它们分配给当前的Point2D实例。我做到了!但是,它返回对象点2d的地址,我想返回参数xc的值,yc@JustAnotherDeveloper-谢谢。我同意你的评论,我也更新了答案。事实上最初我打算发布与我发布的更新相同的内容,但后来我认为这是OP在调试后可以解决的另一个问题。@MukulChikhalkar您可以通过调用相应的getters@MukulChikhalkar-您需要提供toString的实现,如答案所示。这个包裹与此无关。