构造函数重载时重用代码? [复制]

Posted

技术标签:

【中文标题】构造函数重载时重用代码? [复制]【英文标题】:Re-using code when Constructor overloading? [duplicate] 【发布时间】:2015-12-08 02:57:23 【问题描述】:

所以一开始,我有以下构造函数。

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】:

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

public WaitPanelThread( ... parameters...) : this(..parameters for another constructor..)

     // initialize the optional parameters here

【讨论】:

非常感谢。我认为这正是我所需要的。所以当我使用它时,第一个构造函数将像往常一样运行分配值,然后我的新参数也将被分配?另外,初始化对象时的顺序如何?与第一个构造函数相同的顺序后跟指定的新顺序?谢谢! 对,第一次调用会调用this(...)指定的构造函数,然后会执行//initialize the optional parameters here部分。构造函数的this(..) 部分总是首先被调用。如果有更多问题,请阅读构造函数重载的主题。 谢谢你,你帮了大忙。

以上是关于构造函数重载时重用代码? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

C++复制构造函数和=号重载问题

c++中拷贝构造函数和赋值运算符重载本质上一样么

C ++中的复制构造函数和=运算符重载:通用函数可能吗?

c++ 拷贝构造函数与赋值运算符重载函数的区别是

函数重载与复制构造函数

复制构造函数和重载的加法运算符