ファイルを適切なフォルダーに移動するためのメソッドを持つ基本クラスがあります。多くの異なる命名スキームを持つ多くの異なるファイルがあります。移動とフォルダの作成はどのファイルも同じですが、ファイル名が異なるため、日付の決定は異なります。私はこれをやろうとしています:
public class FileBase
{
protected FileInfo _source;
protected string GetMonth()
{
// 2/3 Files have the Month in this location
// So I want this to be used unless a derived class
// redefines this method.
return _source.Name.Substring(Source.Name.Length - 15, 2);
}
public void MoveFileToProcessedFolder()
{
MoveFileToFolder(Properties.Settings.Default.processedFolder + GetMonth);
}
private void MoveFileToFolder(string destination)
{
....
}
}
public class FooFile : FileBase
{
protected new string GetMonth()
{
return _source.Name.Substring(Source.Name.Length - 9, 2);
}
}
public class Program
{
FooFile x = new FooFile("c:\Some\File\Location_20110308.txt");
x.MoveFileToProcessedFolder();
}
問題は、このコードにより、「MoveFileToProcessedFolder」メソッド内で「GetMonth」の基本クラス バージョンが呼び出されることです。「new」キーワードを使用すると、元の実装が隠され、派生実装が引き継ぐことができると思いました。これは起こっていることではありません。明らかに、この場合の new の目的を理解していません。誰かがこれを理解するのを手伝ってくれますか?
ありがとう。