0

そのため、最初は次のコンストラクターがありました。

public WaitPanelThread(Point origin,
                       int delay,
        //bool westEast,
                       String direction,
                       Panel panel,
                       Color colour,
                       Semaphore semaphore,
                       Semaphore semaphore2,
                       Semaphore parkSemaphore,
                       Semaphore nextSlot,
                       Buffer buffer,
                       Buffer buffer2,
                       Buffer parkBuffer,
                       String panelParkSpot,
                       String nextSpot)
    {
        this.origin = origin;
        this.delay = delay;
        this.nextSpot = nextSpot;
        this.parkBuffer = parkBuffer;
        this.nextSlot = nextSlot;
        this.panelParkSpot = panelParkSpot;
        //this.westEast = westEast;
        this.direction = direction;
        this.parkSemaphore = parkSemaphore;

        this.panel = panel;
        this.colour = colour;
        this.plane = origin;
        if (direction == "down")
        {
            this.yDelta = 2;
            this.xDelta = 0;
        }
        if (direction == "left")
        {
            this.xDelta = -10;
            this.yDelta = 0;
        }
        if (direction == "right")
        {
            this.xDelta = 10;
            this.yDelta = 0;
        }
        this.panel.Paint += new PaintEventHandler(this.panel_Paint);
        //this.xDelta = westEast ? +10 : -10;
        //this.yDelta = 0;
        this.semaphore = semaphore;
        this.semaphore2 = semaphore2;
        this.buffer = buffer;
        this.buffer2 = buffer2;

    }

そして、オブジェクトによっては、さらに 3 つのパラメーターが必要になることに気付きました。そこで、さらに 3 つのパラメーターを使用してコンストラクターをオーバーロードし、コードをコピーして貼り付け、追加のパラメーターも割り当てました。

 public WaitPanelThread(Point origin,
                       int delay,
        //bool westEast,
                       String direction,
                       Panel panel,
                       Color colour,
                       Semaphore semaphore,
                       Semaphore semaphore2,
                       Semaphore parkSemaphore,
                       Semaphore nextSlot,
                       Buffer buffer,
                       Buffer buffer2,
                       Buffer parkBuffer,
                       String panelParkSpot,
                       String nextSpot,
                       bool isTurn,
                       Semaphore altSem,
                       Buffer altBuf)
    {
        this.origin = origin;
        this.delay = delay;
        this.nextSpot = nextSpot;
        this.parkBuffer = parkBuffer;
        this.nextSlot = nextSlot;
        this.panelParkSpot = panelParkSpot;
        //this.westEast = westEast;
        this.direction = direction;
        this.parkSemaphore = parkSemaphore;
        this.isTurn = isTurn;

        this.panel = panel;
        this.colour = colour;
        this.plane = origin;
        this.altSem = altSem;
        this.altBuf = altBuf;
        if (direction == "down")
        {
            this.yDelta = 2;
            this.xDelta = 0;
        }
        if (direction == "left")
        {
            this.xDelta = -10;
            this.yDelta = 0;
        }
        if (direction == "right")
        {
            this.xDelta = 10;
            this.yDelta = 0;
        }
        this.panel.Paint += new PaintEventHandler(this.panel_Paint);
        //this.xDelta = westEast ? +10 : -10;
        //this.yDelta = 0;
        this.semaphore = semaphore;
        this.semaphore2 = semaphore2;
        this.buffer = buffer;
        this.buffer2 = buffer2;

    }

ご覧のとおり、2 つのコンストラクターは、3 つの余分なパラメーター (1 つのブール値、1 つのセマフォ、1 つのバッファー) を除いて、その実装はほとんど同じです。私が知りたいのは、オーバーロードされたコンストラクターにすべてのコードを記述する代わりに最初のコンストラクターを参照し、追加のパラメーターに対して追加のコードを記述するだけでよい方法はありますか?

継承されたクラスで「Super()」メソッドを使用するようなことについて話している(調べたところ、同じクラス内にあるため、ここでは実行できません)。

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

4

1 に答える 1

0

その構文は(コンストラクターで)次のとおりです。

public WaitPanelThread( ... parameters...) : this(..parameters for another constructor..)
{
     // initialize the optional parameters here
}
于 2015-09-11T14:04:27.677 に答える