首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

用 C# 編寫網路電話 (7):CaptureSound 多重建構子完整實作

     編輯:關於C語言

四種建構子多載

#region 建構函數
public CaptureSound()
{
    InitializeCommon();
    InitializeDeviceSelector();
    InitializeWaveFormatSelector();
    Initialize();
}

public CaptureSound(Capture device)
{
    InitializeCommon();
    selectedDevice = device;
    InitializeWaveFormatSelector();
    Initialize();
}

public CaptureSound(WaveFormat waveFormat)
{
    InitializeCommon();
    selectedFormat = waveFormat;
    InitializeDeviceSelector();
    Initialize();
}

public CaptureSound(Capture device, WaveFormat waveFormat)
{
    InitializeCommon();
    selectedDevice = device;
    selectedFormat = waveFormat;
    Initialize();
}
#endregion

// 共用初始化邏輯
private void InitializeCommon()
{
    isRecording = false;
    positionNotify = new BufferPositionNotify[5];
    notificationEvent = null;
    buffer = null;
    fileName = string.Empty;
    notify = null;
    notifyThread = null;
    waveFile = null;
    writer = null;
    captureBufferSize = 0;
    nextCaptureOffset = 0;
    sampleCount = 0L;
    notifySize = 0;
}

設計重點:
• 共用欄位初始化提煉至 InitializeCommon() 避免重複。
• 支援裝置/格式任意組合,滿足不同錄音場景需求。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved