java中的方法问题

Mal*_*ent 4 java methods

我正在学习创建自定义类,无法弄清楚我出错的地方

从主班......

MyPoint p1 = new MyPoint(317, 10);

错误说:

constructor MyPoint in class MyPoint cannot be applied to given types;

required: no arguments
found: int, int
reason: actual and formal argument lists differ in length
Run Code Online (Sandbox Code Playgroud)

这是来自我的MyPoint类:

private int x, y;

public void MyPoint(int x, int y)
{
    this.x = x;
    this.y = y;
}
Run Code Online (Sandbox Code Playgroud)

为什么MyPoint(317,10)不与x和y值一起被送入相关类?

任何帮助将不胜感激,谢谢.

Jig*_*shi 5

从中删除返回类型

public void MyPoint(int x, int y)
Run Code Online (Sandbox Code Playgroud)

构造函数不能有返回类型 void

做了

public MyPoint(int x, int y)
{
    this.x = x;
    this.y = y;
}
Run Code Online (Sandbox Code Playgroud)


Gau*_*pta 5

构造函数没有返回类型.这只是您刚刚制作的常规方法.

解决方案:void从方法中删除.应该是这样的

public MyPoint(int x, int y)
{
    this.x = x;
    this.y = y;
}
Run Code Online (Sandbox Code Playgroud)