我正在尝试编写一个公共(public)实例方法名称move()
它需要两个整数参数,显示对象需要更改 xPos 和 yPos 值的数量。
我不希望该方法返回值。
我已在下面执行此操作,但收到以下错误消息?
编译失败 (18/01/2020 15:16:31) 错误:第 1 行 - StickFigure 类中的方法 move 无法应用于给定类型; 必需:无参数 发现:int,int 原因:实际参数列表和形式参数列表的长度不同
我可以在哪里出错的地方得到一些指导吗?
/*Instance variables*/
private int xPos;
private int yPos;
private Circle head;
private Triangle body;
private Rectangle leg;
public person()
{
super();
this.head = new Circle(30, OUColour.PINK);
this.body = new Triangle (50, 50, OUColour.RED);
this.leg = new Rectangle (6, 50, OUColour.PINK);
this.setXPos(25);
this.setYPos(220);
this.alignAll();
}
public void setXPos(int newPos)
{
this.xPos = newPos;
this.body.setXPos(newPos);//part (b)(iii)
}
public int getXPos()
{
return this.xPos;
}
public void setYPos(int newPos)
{
this.yPos = newPos;
this.body.setYPos(newPos);//part (b)(iii)
}
public int getYPos()
{
return this.yPos;
}
public Circle getHead()
{
return this.head;
}
public Triangle getBody()
{
return this.body;
}
public Rectangle getLeg()
{
return this.leg;
}
public void alignHead()
{
this.head.setXPos(this.body.getXPos() + (this.body.getWidth() - this.head.getDiameter())/2);
this.head.setYPos(this.body.getYPos() - this.head.getDiameter());
}
public void alignBody()
{
this.body.setXPos(25);
this.body.setYPos(220);
}
public void alignLeg()
{
this.leg.setXPos(this.body.getXPos() + (this.body.getWidth() - this.leg.getWidth())/2);
this.leg.setYPos(this.body.getYPos() + this.leg.getHeight());
}
public void alignAll()
{
this.alignBody();
this.alignHead();
this.alignLeg();
}
public void move(int newxPos, int newyPos)
{
this.body.setXPos(xPos + newxPos);
this.body.setYPos(yPos + newyPos);
this.alignAll();
this.delay(20);
}
最佳答案
如果您希望方法 move
接受参数,则必须声明它:
public void move(int xPos, int yPos)
而不是
public void move()
关于java - 移动 3 个对象但保持它们对齐的公共(public)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59802047/