的Java:不兼容的类型,在方法的参数
问题描述:
我想回到这个方法:的Java:不兼容的类型,在方法的参数
private int x;
private int y;
private double pointOne;
private double pointTwo;
public Point (int newX , int newY)
{
this.x = newX;
this.y = newY;
}
public Point halfwayTo (Point other)
{
pointOne = ((this.x + other.x)/2);
pointTwo = ((this.y + other.y)/2);
return new Point ("(" + pointOne + "," + pointTwo + ")");
}
一个新的点是半路给定的两个点(中点公式)的。
但是,我收到一个错误“不兼容的类型:字符串不能转换为点”。
我需要返回两个新点作为(X,Y)。
答
带有一个参数的Point的唯一构造函数需要Point对象。
你传递一个字符串:"(" + pointOne + "," + pointTwo + ")"
因此有消息异常“串不能转换为点”。
请注意,有一个constructor需要两个整数,您可能想要使用这个。
根据您的编辑,您正在使用自定义的Point
对象。
检查你的进口,你可能正在使用java.awt.Point
,而你认为你使用自定义Point
对象。
Nvm,我在看我的Parameterized构造函数。我有另一个只有一个参数的副本构造函数。好的,我现在得到了你想说的话。感谢您的帮助。 – Paincakes