C# 构造函数重载(使用 this 关键字)
C# Constructor Overloading (using this keyword)
我正在尝试学习 C#,但遇到了一个问题。
对于大多数有经验的程序员来说可能是显而易见的,但对我来说不是:(
我正在尝试编写一个应该有 3 个构造函数重载的 class:
- 我只指定文件夹
- 我指定文件夹和文件类型(文件扩展名)。
- 我指定文件夹、文件类型和yes/no用于子文件夹搜索。
根据用户指定 class returns 列表。
所以我写了下面的代码:
public class SearchForFiles
{
private readonly List<string> _filePath = new List<string>();
private readonly List<string> _fileType = new List<string>();
private readonly bool _searchSubFolder = false;
private SearchOption _searchOption = SearchOption.TopDirectoryOnly;
private readonly IEnumerable<string> _searchForFiles = null;
/// <summary>
/// Searching all the files in the specified folder.
/// </summary>
/// <param name="searchFolder">Specify folder for file search</param>
public SearchForFiles(string searchFolder)
{
_filePath.Add(searchFolder);
_fileType.Add(".*");
FindFiles(_filePath, _fileType, false);
}
/// <summary>
/// Searching all the files with the specified file extension in the specified folder.
/// </summary>
/// <param name="searchFolder">Specify folder for file search</param>
/// <param name="fileType">Specify extension of the file search</param>
public SearchForFiles(string searchFolder, string fileType)
: this(searchFolder)
{
_fileType.Add(fileType);
FindFiles(_filePath, _fileType, false);
}
public SearchForFiles(List<string> searchFolder, List<string> fileType, bool searchSubFolder)
: this(searchFolder, fileType)
{
_searchSubFolder = searchSubFolder;
FindFiles(_filePath, _fileType, _searchSubFolder);
}
问题是第三个重载,我不明白为什么第二个重载可以,第三个却不行。
如果有人能给我一些帮助那就太好了。
这是有效的更新代码:
/// <summary>
/// Returning a list of files depending on the user choices.
/// </summary>
public class SearchForFiles
{
private readonly List<string> _filePath = new List<string>();
private readonly List<string> _fileType = new List<string>();
private readonly bool _searchSubFolder = false;
private SearchOption _searchOption = SearchOption.TopDirectoryOnly;
private IEnumerable<string> _searchForFiles = null;
/// <summary>
/// Searching all the files with the specified file extension(s) in the specified folder(s) and sub folder(s) if wanted.
/// </summary>
/// <param name="searchFolder">Specify folder(s) for file(s) search</param>
/// <param name="fileType">Specify file extension(s) for the search</param>
/// <param name="searchSubFolder">Specify if you want to search in sub folder(s)</param>
public SearchForFiles(List<string> searchFolder, List<string> fileType, bool searchSubFolder)
{
_filePath = searchFolder;
_fileType = fileType;
_searchSubFolder = searchSubFolder;
FindFiles();
}
/// <summary>
/// Searching all the files with the specified file extension in the specified folder.
/// </summary>
/// <param name="searchFolder">Specify folder for file search</param>
/// <param name="fileType">Specify file extension for the search</param>
public SearchForFiles(string searchFolder, string fileType)
: this(new List<string>() {searchFolder}, new List<string>() {fileType}, false)
{
}
/// <summary>
/// Searching all the files in the specified folder.
/// </summary>
/// <param name="searchFolder">Specify folder for file search</param>
public SearchForFiles(string searchFolder)
: this(new List<string>() {searchFolder}, new List<string>(), false)
{
}
private List<string> FindFiles()
{
var findFiles = new List<string>();
if (_searchSubFolder == true)
_searchOption = SearchOption.AllDirectories;
foreach (var filePath in _filePath)
{
if (_fileType.Count == 0)
{
_searchForFiles = Directory.EnumerateFiles(filePath, "*.*", _searchOption);
}
else
{
_searchForFiles = Directory.EnumerateFiles(filePath, "*.*", _searchOption).Where(e => _fileType.Contains(new FileInfo(e).Extension, StringComparer.OrdinalIgnoreCase));
}
foreach (var file in _searchForFiles)
{
findFiles.Add(file);
}
}
return findFiles;
}
}
非常感谢所有的提示和回答:)
@CrazyNoun,正如@D-Shih 所说,您必须首先纠正类型不匹配问题。
关于构造函数:构造函数重载可以实现以多种方式实现单个目标(例如 MessageBox.Show() windows 形式的重载)并且它可以实现通过多种方式实现多个目标(例如 Convert.ToString() 重载).
您想实现一个目标。这样我们应该定义 Defaults。
您的声明处于 Reverse 顺序。实际上你必须这样定义:
public class SearchForFiles
{
public SearchForFile(List<string> searchFolder, List<string> fileType, bool searchSubFolder)
{
_searchSubFolder = searchSubFolder;
FindFiles(searchFolder, fileType, searchSubFolder);
}
public SearchForFiles(string searchFolder, string fileType):this(new List<string>(){searchFolder}, new List<string>(){fileType}, false)
{
}
public SearchForFiles(string searchFolder):this(new List<string>(){searchFolder}, new List<string>(), false)
{
}
我已经为构造函数添加了 List 支持。所以如果你想概括它是必要的。如您所见,无需为每个构造函数签名编写主体。实际上,其他构造函数将为主构造函数创建一些输入参数,他们将通过放置值并添加一些 默认值 来调用它 运行 以便更快地使用。
我正在尝试学习 C#,但遇到了一个问题。 对于大多数有经验的程序员来说可能是显而易见的,但对我来说不是:(
我正在尝试编写一个应该有 3 个构造函数重载的 class:
- 我只指定文件夹
- 我指定文件夹和文件类型(文件扩展名)。
- 我指定文件夹、文件类型和yes/no用于子文件夹搜索。
根据用户指定 class returns 列表。
所以我写了下面的代码:
public class SearchForFiles
{
private readonly List<string> _filePath = new List<string>();
private readonly List<string> _fileType = new List<string>();
private readonly bool _searchSubFolder = false;
private SearchOption _searchOption = SearchOption.TopDirectoryOnly;
private readonly IEnumerable<string> _searchForFiles = null;
/// <summary>
/// Searching all the files in the specified folder.
/// </summary>
/// <param name="searchFolder">Specify folder for file search</param>
public SearchForFiles(string searchFolder)
{
_filePath.Add(searchFolder);
_fileType.Add(".*");
FindFiles(_filePath, _fileType, false);
}
/// <summary>
/// Searching all the files with the specified file extension in the specified folder.
/// </summary>
/// <param name="searchFolder">Specify folder for file search</param>
/// <param name="fileType">Specify extension of the file search</param>
public SearchForFiles(string searchFolder, string fileType)
: this(searchFolder)
{
_fileType.Add(fileType);
FindFiles(_filePath, _fileType, false);
}
public SearchForFiles(List<string> searchFolder, List<string> fileType, bool searchSubFolder)
: this(searchFolder, fileType)
{
_searchSubFolder = searchSubFolder;
FindFiles(_filePath, _fileType, _searchSubFolder);
}
问题是第三个重载,我不明白为什么第二个重载可以,第三个却不行。
如果有人能给我一些帮助那就太好了。
这是有效的更新代码:
/// <summary>
/// Returning a list of files depending on the user choices.
/// </summary>
public class SearchForFiles
{
private readonly List<string> _filePath = new List<string>();
private readonly List<string> _fileType = new List<string>();
private readonly bool _searchSubFolder = false;
private SearchOption _searchOption = SearchOption.TopDirectoryOnly;
private IEnumerable<string> _searchForFiles = null;
/// <summary>
/// Searching all the files with the specified file extension(s) in the specified folder(s) and sub folder(s) if wanted.
/// </summary>
/// <param name="searchFolder">Specify folder(s) for file(s) search</param>
/// <param name="fileType">Specify file extension(s) for the search</param>
/// <param name="searchSubFolder">Specify if you want to search in sub folder(s)</param>
public SearchForFiles(List<string> searchFolder, List<string> fileType, bool searchSubFolder)
{
_filePath = searchFolder;
_fileType = fileType;
_searchSubFolder = searchSubFolder;
FindFiles();
}
/// <summary>
/// Searching all the files with the specified file extension in the specified folder.
/// </summary>
/// <param name="searchFolder">Specify folder for file search</param>
/// <param name="fileType">Specify file extension for the search</param>
public SearchForFiles(string searchFolder, string fileType)
: this(new List<string>() {searchFolder}, new List<string>() {fileType}, false)
{
}
/// <summary>
/// Searching all the files in the specified folder.
/// </summary>
/// <param name="searchFolder">Specify folder for file search</param>
public SearchForFiles(string searchFolder)
: this(new List<string>() {searchFolder}, new List<string>(), false)
{
}
private List<string> FindFiles()
{
var findFiles = new List<string>();
if (_searchSubFolder == true)
_searchOption = SearchOption.AllDirectories;
foreach (var filePath in _filePath)
{
if (_fileType.Count == 0)
{
_searchForFiles = Directory.EnumerateFiles(filePath, "*.*", _searchOption);
}
else
{
_searchForFiles = Directory.EnumerateFiles(filePath, "*.*", _searchOption).Where(e => _fileType.Contains(new FileInfo(e).Extension, StringComparer.OrdinalIgnoreCase));
}
foreach (var file in _searchForFiles)
{
findFiles.Add(file);
}
}
return findFiles;
}
}
非常感谢所有的提示和回答:)
@CrazyNoun,正如@D-Shih 所说,您必须首先纠正类型不匹配问题。 关于构造函数:构造函数重载可以实现以多种方式实现单个目标(例如 MessageBox.Show() windows 形式的重载)并且它可以实现通过多种方式实现多个目标(例如 Convert.ToString() 重载).
您想实现一个目标。这样我们应该定义 Defaults。 您的声明处于 Reverse 顺序。实际上你必须这样定义:
public class SearchForFiles
{
public SearchForFile(List<string> searchFolder, List<string> fileType, bool searchSubFolder)
{
_searchSubFolder = searchSubFolder;
FindFiles(searchFolder, fileType, searchSubFolder);
}
public SearchForFiles(string searchFolder, string fileType):this(new List<string>(){searchFolder}, new List<string>(){fileType}, false)
{
}
public SearchForFiles(string searchFolder):this(new List<string>(){searchFolder}, new List<string>(), false)
{
}
我已经为构造函数添加了 List 支持。所以如果你想概括它是必要的。如您所见,无需为每个构造函数签名编写主体。实际上,其他构造函数将为主构造函数创建一些输入参数,他们将通过放置值并添加一些 默认值 来调用它 运行 以便更快地使用。