-4

クラスから別のクラスにゲッターとセッターを呼び出すにはどうすればよいですか? Ball.cs から StartGame.cs という名前の別のクラスを呼び出す必要があります。StartGame.cs のタイマーに入れる必要があります。たとえば、クラス Ball.

public class Ball
{
    public int speedX { get; private set; }
    public int speedY { get; private set; }
    public int positionX { get; private set; }
    public int positionY { get; private set; }

    public Ball(int speedX, int speedY, int positionX, int positionY)
    {
        this.speedX = speedX;
        this.speedY = speedY;
        this.positionX = positionX;
        this.positionY = positionY;
    }

    public int setSpeedX(int newSpeedX)
    {
        speedX = newSpeedX;
        return newSpeedX;
    }

    public int setSpeedY(int newSpeedY)
    {
        speedY = newSpeedY;
        return newSpeedY;
    }

    public int setPositionX(int newPositionX)
    {
        positionX = newPositionX;
        return newPositionX;
    }

    public int setPositionY(int newPositionY)
    {
        positionY = newPositionY;
        return newPositionY;
    }
}

ありがとうございました。

4

4 に答える 4

1

別のクラスで変数を使用する場合は、その変数を public (または、他のクラスから継承している場合は protected/protected internal) として定義する必要があります。

ただし、そのように変数を公開することは、クラスの実装を公開することを意味します。そのようなものを抽象化し、get および set アクセサーを使用してプロパティを介して変数を公開するのが最善です。

于 2013-05-22T09:19:53.967 に答える