简体   繁体   English

扩展基类方法

[英]Extending a base class method

I am new to C# and am trying to understand basic concepts. 我是C#的新手,正在尝试了解基本概念。 Thank you in advance for your help. 预先感谢您的帮助。 I have some sample classes below (typed in this window so there may be some errors)and have two questions: 我下面有一些示例类(在此窗口中键入,因此可能会有一些错误),并且有两个问题:

  1. Is it possible to Call a derived class method that executes the code in the base class method with the same name, then executes the code in the derived class method? 是否可以调用派生类方法,以相同的名称执行基类方法中的代码,然后执行派生类方法中的代码? Every derived class will need to perform the base class code for the RunCheck then do specialized code specific to its class. 每个派生类都需要执行RunCheck的基类代码,然后执行特定于其类的专用代码。 I could name RunCheck() something else in the base class and then call it when I call the RunCheck() of the derived class but then I have to remember to call it on the RunCheck() in the derived class. 我可以在基类中为RunCheck()命名其他名称,然后在我调用派生类的RunCheck()时调用它,但是我必须记住要在派生类的RunCheck()上调用它。

  2. In the Program.cs I want to output all fields with a blank value if it is on a field that is not in the derived class I pass in. What would I pass in? 在Program.cs中,如果要输入的字段不在我传入的派生类中,则我希望输出具有空白值的所有字段。我应该输入什么?

Here is my code: 这是我的代码:

  class baseCheck
  {
      public DateTime StartTime { get; set; }
      public DateTime LastRun { get; set; }
      public int Runs { get; set; }
      //Others

      public void RunCheck()
      {
         if (Started != null)
           started = DateTime.Now;

         LastRun = DateTime.Now;

         Runs++;
      }
    }

    class FileCheck : baseCheck
    {
       public string FileName { get; set; }

       public void RunCheck()
       {
           //I want all the code in the base class to run plus
           //any code I put here when calling this class method

        }
    }
    class DirectoryCheck : baseCheck
    {
       public string DirectoryName { get; set; }

       public void RunCheck()
       {
           //I want all the code in the base class to run plus
           //any code I put here when calling this class method

        }
    }

        //Program.cs
        static void Main()
        {
           //Create derived class - either DirectoryCheck or FileCheck
           //depending on what the user chooses.

            if (Console.ReadLine()=="F")
            {
                FileCheck c = new FileCheck();  
            }
            else
            {
                DirectoryCheck c = new DirectoryCheck();
            }

            PrintOutput(c);

        }
        private void PrintOut(What do I put here?)
        {
           Console.WriteLine("Started: {0}",f.StartTime)
           Console.WriteLine("Directory: {0}", f.DirectoryName)
           Console.WriteLine("File: {0}", f.FileName}
        }

Just call base.RunCheck() in your DirectoryCheck class: 只需在DirectoryCheck类中调用base.RunCheck()

public class DirectoryCheck : baseCheck
{
    public string DirectoryName { get; set; }

    public void RunCheck()
    {
        //I want all the code in the base class to run plus
        //any code I put here when calling this class method
        base.RunCheck();
        Console.WriteLine("From DirectoryCheck");
    }
}

Also with your current implementation you are hiding the base class RunCheck() method - you should really override it - this changes the method signature in the base class to 同样,在当前的实现中,您将隐藏基类RunCheck()方法-您应该覆盖它-这会将基类中的方法签名更改为

    public virtual void RunCheck()

and in the derived classes to 并在派生类中

    public override void RunCheck()

I suspect what you really want though is something like the Non Virtual interface pattern (NVI) - Expose a protected virtual method in your base class, that child classes can override, but have a public method on the base class that is actually calling that method internally - this approach allows you to extend what you are doing before and after that call. 我怀疑您真正想要的是非虚拟接口模式(NVI)-在基类中公开受保护的虚拟方法,子类可以覆盖,但在基类上有一个实际调用该方法的公共方法内部-这种方法可让您扩展该呼叫之前和之后的工作。

In your example this would look like this: 在您的示例中,它看起来像这样:

class BaseCheck
{
    private DateTime Started { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime LastRun { get; set; }
    public int Runs { get; set; }
    //Others

    public void RunCheck()
    {
        if (Started != null)
            Started = DateTime.Now;

        LastRun = DateTime.Now;
        Runs++;
        CoreRun();
    }

    protected virtual void CoreRun()
    {

    }
}


public class DirectoryCheck : BaseCheck
{
    public string DirectoryName { get; set; }

    protected override void CoreRun()
    {
        //I want all the code in the base class to run plus
        //any code I put here when calling this class method
        Console.WriteLine("From DirectoryCheck");
    }
}

In a derived class, you can call the method in the base class using: 在派生类中,可以使用以下方法在基类中调用方法:

public override void RunCheck()
{
    base.RunCheck();

    // Followed by the implementation of the derived class
}

As mentioned in the comments, the base method will need to be declared as virtual to allow overriding: 如评论中所述,基本方法将需要声明为virtual以允许覆盖:

public virtual void RunCheck() { ... }

For your PrintOut() method, there is no magic way, but you could have it take the base class as a parameter, and then test for the type. 对于您的PrintOut()方法,没有神奇的方法,但是您可以让它将基类作为参数,然后测试类型。

private void PrintOut(baseCheck f)
{
   Console.WriteLine("Started: {0}", f.StartTime)
   Console.WriteLine("Directory: {0}", f.DirectoryName)

   if (check is FileCheck)
   {
       Console.WriteLine("File: {0}", ((FileCheck)f).FileName}
   }
}

Or you could use overloads: 或者您可以使用重载:

private void PrintOut(baseCheck f)
{
   Console.WriteLine("Started: {0}", f.StartTime)
   Console.WriteLine("Directory: {0}", f.DirectoryName)
}

private void PrintOut(FileCheck f)
{
    PrintOut((baseCheck)f);

    Console.WriteLine("File: {0}", ((FileCheck)f).FileName}
}

Or you could have your PrintOut method part of your class (maybe even use the existing ToString() method) and override it as required. 或者,您可以将PrintOut方法作为类的一部分(甚至可以使用现有的ToString()方法)并根据需要覆盖它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM