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;

    }

然后我意识到,对于某些对象,我还需要三个参数。所以我用另外三个参数重载了构造函数,复制粘贴代码并分配了额外的参数。

 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;

    }

如您所见,除了三个额外的参数(一个布尔值、一个信号量和一个缓冲区)之外,这两个构造函数的实现大部分相同。我想知道的是,与其在重载的构造函数中编写所有代码,有没有办法引用第一个构造函数并且只需要为额外的参数编写额外的代码?

我说的是在继承的类中使用“Super()”方法之类的东西(我研究了一下,这里不能这样做,因为它们在同一个类中)。

谢谢你。

4

1 回答 1

0

其语法是(在构造函数中):

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