c# - 扩展基类方法

标签 c# .net inheritance extension-methods

我是 C# 的新手,正在尝试了解基本概念。预先感谢您的帮助。我在下面有一些示例类(在此窗口中输入,因此可能存在一些错误)并有两个问题:

  1. 是否可以调用派生类方法执行同名基类方法中的代码,然后执行派生类方法中的代码?每个派生类都需要为 RunCheck 执行基类代码,然后执行特定于其类的专门代码。我可以在基类中将 RunCheck() 命名为其他名称,然后在调用派生类的 RunCheck() 时调用它,但我必须记住在派生类中的 RunCheck() 上调用它。

  2. 在 Program.cs 中,如果它位于不在我传入的派生类中的字段上,我想输出所有具有空白值的字段。我会传入什么?

    <

这是我的代码:

  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}
        }

最佳答案

只需在您的 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");
    }
}

此外,对于您当前的实现,您还隐藏了基类 RunCheck() 方法 - 您真的应该覆盖它 - 这会将基类中的方法签名更改为

    public virtual void RunCheck()

并在派生类中

    public override void RunCheck()

我怀疑您真正想要的是 Non Virtual interface 之类的东西pattern (NVI) - 在你的基类中公开一个 protected 虚方法,子类可以重写,但在基类上有一个公共(public)方法,它实际上在内部调用该方法 - 这种方法允许你扩展你之前所做的和那个电话之后。

在您的示例中,它看起来像这样:

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");
    }
}

关于c# - 扩展基类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5400549/

相关文章:

c# - 好的类(class)名称的想法

使用参数和可选值的 C# 方法重载

Java 接口(interface)合成方法生成,同时缩小返回类型

java - Lombok @EqualsAndHashCode 与 Scala - 案例类不能继承 Java 类

java - 如何使扩展另一个扩展类的类与通用集合一起使用?

c# - 术语 "pending dispatch"是什么意思?

c# - Windows Phone 7 中的音效

c# - 如何使用 Dapper 映射 MySQL JSON 列

asp.net - <authorization> 节点的 Web.config 错误

.net - 构建时如何在 .NET 依赖链上要求 x86