1

ファイルを適切なフォルダーに移動するためのメソッドを持つ基本クラスがあります。多くの異なる命名スキームを持つ多くの異なるファイルがあります。移動とフォルダの作成はどのファイルも同じですが、ファイル名が異なるため、日付の決定は異なります。私はこれをやろうとしています:

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 の目的を理解していません。誰かがこれを理解するのを手伝ってくれますか?

ありがとう。

4

4 に答える 4

5

メソッドを仮想としてマークし、派生クラスでオーバーライドします。New を使用するとアイテムのシグネチャを変更できるため、基本クラスに void DoWork() という名前のメソッドがある場合、 new キーワードを使用して派生クラスで int DoWork() を宣言できます。これにより、暗黙的な呼び出しは解決されますが、基本クラスのメソッドを明示的に呼び出すことはできます。

仮想 (ベース) とオーバーライド (派生) を使用する

于 2011-03-08T18:07:33.000 に答える
4

本当に必要なのは、基本クラスのメソッドを作成してvirtualからoverrideサブクラスにすることです。

public class BaseClass {
    public virtual int Foo() {
        return 1;
    }
}

public class SubClass : BaseClass {
    public override int Foo() {
        return 42;
    }
}
于 2011-03-08T18:08:07.463 に答える
3

メソッドを非表示にしているタイプによって直接参照されている場合にのみ非表示になります。ただし、基本クラスから実装を呼び出しているため、そこで定義されているメソッドを延期しています。

あなたの場合、メソッドを隠すのではなく、仮想実装が必要なようです。

public class FileBase
{
   protected FileInfo _source;

   protected virtual 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 override 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();
}
于 2011-03-08T18:09:22.953 に答える
1

この場合Virtual、基本クラスとOverride派生クラスで使用する必要があります。以下のようにすれば、`new' を使って期待通りに動作します。

class Program
    {
        static void Main(string[] args)
        {
            FileBase fb = new FileBase();
            Console.WriteLine(fb.GetMonth());

            FooFile ff = new FooFile();
            Console.WriteLine(ff.GetMonth());

            Console.ReadLine();

        }
    }

   public class FileBase
   {
       public string GetMonth()
       {
           return "FileBase::GetMonth()";
       }

    }

    public class FooFile : FileBase
    {
        public new string GetMonth() // Hides the base method
       {
           return "FooFile::GetMonth()";
       }
    }
于 2011-03-08T18:35:21.827 に答える