java中的方法问题
我正在学习创建自定义类,但不知道哪里出了问题 从主课java中的方法问题,java,methods,Java,Methods,我正在学习创建自定义类,但不知道哪里出了问题 从主课 mypointp1=新的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 这是我的MyPoint课程: priv
mypointp1=新的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
这是我的MyPoint课程:
private int x,y代码>
public void MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
为什么MyPoint(317,10)没有与x和y值一起输入到相关类中
任何帮助都将不胜感激,谢谢。从中删除退货类型
public void MyPoint(int x, int y)
构造函数不能有返回类型,即使void
成功
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
构造函数没有返回类型。这只是你刚刚做的一个普通方法
解决方案:从方法中删除void
。应该是这样的
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
您需要在MyPoint
类中声明参数化构造函数
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
构造函数不能具有返回类型。因此,将代码更改为
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
因为当构造函数有返回值时?你特别想做什么?给出定义构造函数是初始化时调用的,你已经将它定义为方法。我想所有的答案都是正确的。啊,返回语句完全隔开了-一定是把它和其他工作混淆了。谢谢