构造函数重载时重用代码?

时间:2015-09-11 13:59:58

标签: c# constructor-overloading

首先,我有以下构造函数。

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()"继承类中的方法(我查看过它,不能在这里完成,因为它们在同一个类中)。

谢谢。

1 个答案:

答案 0 :(得分:0)

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

public WaitPanelThread( ... parameters...) : this(..parameters for another constructor..)
{
     // initialize the optional parameters here
}