Java 类的构造函数
当我尝试创建类的新实例 Point
Point nir = new Point(double x, double y);
我收到错误,
Multiple markers at this line
- x cannot be resolved to a variable
- y cannot be resolved to a variable
这是怎么回事?我希望 x 和 y 是通用的,而不是具体的。我想将我的 Point
包含为新 class
的字段。
编辑:
在给定的名为 Circle
的 class
中,我想替换表示坐标的字段 x0
和 y0
一个点,具有 Point
类型的对象。所以这是我想要重构的类 Circle
的开头,如下所示:
public class Circle {
private double x0, y0, radius;
所以,我基本上想更改 x0
、y0
的表示> 到 Point
结构。
When I try to create a new instance of the class Point
Point nir = new Point(double x, double y);
I'm getting the error
Multiple markers at this line
- x cannot be resolved to a variable
- y cannot be resolved to a variable
How come? I want x
and y
to be general, not specific. I want to include my Point
as a field of a new class
.
EDIT:
In a given class
named Circle
I want to replace the fields x0
and y0
, that represent the coordinates of a point, with an object of type Point
. So this is the begining of the class Circle
that I want to refactor as above:
public class Circle {
private double x0, y0, radius;
So, I basically want to change the representation of x0
, y0
to Point
structure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您收到的错误是该代码
不是合法的 Java。创建对象或调用函数时,无需指定参数的类型。相反,您只需提供该类型的值即可。因此,例如,您可以通过编写
Or
创建一个点,因为在这两种情况下,编译器已经知道您作为构造函数参数提供的表达式的类型。你不需要(事实上不应该)说他们是双打。
希望这有帮助!
The error you're getting is that this code
is not legal Java. When you create an object or call a function, you do not specify the types of the arguments. Instead, you just provide a value of that type. So, for example, you could create a point by writing
Or
Because in both cases the compiler already knows the types of the expressions you're providing as constructor arguments. You don't need to (and in fact should not) say that they're doubles.
Hope this helps!
试试这个:
如果这不起作用,请显示更多代码。
Try this:
If that doesn't work, show more code.
您需要像这样创建实例:
或者像这样:
其中 x 和 y 是双精度数。您收到错误是因为调用构造函数时无法指定参数的类型,因此
Point nir = new Point(double x, double y);
会导致错误。You need to create the instance like so:
Or like so:
where x and y are doubles. You're getting an error because you can't specify the type for arguments when calling a constructor, so
Point nir = new Point(double x, double y);
causes an error.x 和 y 必须已经制作:
所以这样做:
x and y have to already made:
so do:
您尝试在需要参数时设置参数。尝试:
或:
You're trying to set the parameters when it's expecting arguments. Try:
Or: