Java 类的构造函数

发布于 2024-10-26 14:42:17 字数 931 浏览 1 评论 0原文

当我尝试创建类的新实例 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 的字段。

编辑:

在给定的名为 Circleclass 中,我想替换表示坐标的字段 x0y0一个点,具有 Point 类型的对象。所以这是我想要重构的类 Circle 的开头,如下所示:

public class Circle {

   private double x0, y0, radius;

所以,我基本上想更改 x0y0 的表示> 到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

江湖彼岸 2024-11-02 14:42:17

您收到的错误是该代码

new Point(double x, double y);

不是合法的 Java。创建对象或调用函数时,无需指定参数的类型。相反,您只需提供该类型的值即可。因此,例如,您可以通过编写

Point origin = new Point(0.0, 0.0);

Or

double x = 137.0;
double y = 2.71828;
Point myPoint = new Point(x, y);

创建一个点,因为在这两种情况下,编译器已经知道您作为构造函数参数提供的表达式的类型。你不需要(事实上不应该)说他们是双打。

希望这有帮助!

The error you're getting is that this code

new Point(double x, double y);

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

Point origin = new Point(0.0, 0.0);

Or

double x = 137.0;
double y = 2.71828;
Point myPoint = new Point(x, y);

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!

内心激荡 2024-11-02 14:42:17

试试这个:

Point nir= new Point(x, y);

如果这不起作用,请显示更多代码。

Try this:

Point nir= new Point(x, y);

If that doesn't work, show more code.

独木成林 2024-11-02 14:42:17

您需要像这样创建实例:

Point nir = new Point(x, y);

或者像这样:

Point nir = new Point(15.0, 12.0);

其中 x 和 y 是双精度数。您收到错误是因为调用构造函数时无法指定参数的类型,因此 Point nir = new Point(double x, double y); 会导致错误。

You need to create the instance like so:

Point nir = new Point(x, y);

Or like so:

Point nir = new Point(15.0, 12.0);

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.

猫烠⑼条掵仅有一顆心 2024-11-02 14:42:17

x 和 y 必须已经制作:

所以这样做:

Point nir = new Point(x, y);

x and y have to already made:

so do:

Point nir = new Point(x, y);
末が日狂欢 2024-11-02 14:42:17

您尝试在需要参数时设置参数。尝试:

Point nir= new Point(x, y);

或:

Point nir= new Point((double) x, (double) y);

You're trying to set the parameters when it's expecting arguments. Try:

Point nir= new Point(x, y);

Or:

Point nir= new Point((double) x, (double) y);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文