我想知道方法之间是否有任何方法可以进行数学加法?代码如下.
从Main :(我需要计算,但我不能在方法之间进行计算)
Ball.setPositionY = Ball.setPositionY + Ball.setSpeedY;
Run Code Online (Sandbox Code Playgroud)
来自一个班级:
public class Ball
{
public int speedX { get; set; }
public int speedY { get; set; }
public int positionX { get; set; }
public int positionY { get; set; }
public Ball(int speedX, int speedY, int positionX, int positionY)
{
this.speedX = speedX;
this.speedY = speedY;
this.positionX = positionX;
this.positionY = positionY;
}
public void setSpeedX(int newSpeedX)
{
speedX = newSpeedX;
}
public void setSpeedY(int newSpeedY)
{
speedY = newSpeedY;
}
public void setPositionX(int newPositionX)
{
positionX = newPositionX;
}
public void setPositionY(int newPositionY)
{
positionY = newPositionY;
}
}
Run Code Online (Sandbox Code Playgroud)
这是你应该这样做的:
public class Ball
{
public int SpeedX { get; set; }
public int SpeedY { get; set; }
public int PositionX { get; set; }
public int PositionY { get; set; }
public Ball(int speedX, int speedY, int positionX, int positionY)
{
this.SpeedX = speedX;
this.SpeedY = speedY;
this.PositionX = positionX;
this.PositionY = positionY;
}
}
public class Program
{
public static void Main(string[] args)
{
Ball ball1 = new Ball(1,1,1,1);
Ball ball2 = new Ball(2,2,2,2);
Ball ball3 = new Ball(3,3,3,3);
ball3.SpeedX = ball1.SpeedX + ball2.SpeedX;
}
}
Run Code Online (Sandbox Code Playgroud)