简体   繁体   English

当调用派生类中的方法时,基类是否有可能获得通知?

[英]Is it possible a base class gets informed when a method in derived class is called?

Consider the following little design: 考虑以下小设计:

public class Parent
{
  public event EventHandler ParentWentOut;
  public virtual void GoToWork()
  {
     ParentWentOut();
  }
}

public class Mother : Parent
{
   public override void GoToWork()
   {
     // Do some stuff here
     base.GoToWork(); // <- I don't want to write this in any derived class. 
                      //    I want base class's method to be automatically called.
   }
}

Is there any mechanism to make Parent.GoToWork method implicitly and automatically be called whenever this method finishes in overridden version of the descendants (here Mother class) ? 有没有任何机制可以隐式地自动调用Parent.GoToWork方法,只要这个方法在后代的重写版本(这里是Mother类)中完成?

If there is any other language than C# able to do so, I'll be very thankful to know. 如果有其他语言而不是C#能够这样做,我将非常感谢知道。

You can try to implement something like this 你可以尝试实现这样的东西

public class Parent
{
   public event EventHandler ParentWentOut;
   public void GoToWork()
   {
     BeforeParentWentOut();
     ParentWentOut();
     AfterParentWentOut();         
   }

   protected virtual void BeforeParentWentOut()
   {
      // Dont do anything, you can even make it abstract if it suits you
   }

   protected virtual void AfterParentWentOut()
   {
      // Dont do anything, you can even make it abstract if it suits you
   }
}



public class Mother : Parent
{
   protected override void BeforeParentWentOut()
   {
      // Do some stuff here
   }
}

Also you can subscribe to your own event on the Mother class and react to that. 您也可以在Mother类上订阅自己的活动并对此作出反应。

EDIT: update for protected, added before/after methods to handle when to add code to the parent implementation 编辑:更新为受保护,添加之前/之后的方法来处理何时向父实现添加代码

I would suggest having two methods - one for the public API and one for the internal functioning of the method. 我建议有两种方法 - 一种用于公共API,另一种用于方法的内部功能。

public class Parent
{
  public event EventHandler ParentWentOut;
  public void GoToWork()
  {
     GoToWorkOverride();
     ParentWentOut();
  }

  protected virtual void GoToWorkOverride()
  {}
}

public class Mother : Parent
{
   protected override void GoToWorkOverride()
   {
       // Do some stuff here
   }
}

So, far the only way to access the base class is through the base keyword which some how hold a reference to base class which is initialized by calling its constructor. 因此,访问基类的唯一方法是通过base关键字,其中一些如何保存对通过调用其构造函数初始化的基类的引用。

Thus your answer is no. 因此你的答案是否定的。

暂无
暂无

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

相关问题 是否可以在派生类中实现抽象方法,但只能在基类中调用抽象方法? - is it possible to allow an abstract method to be implemented in derived class, but only called in the base class? 为什么调用基类方法而不是派生类方法? - Why is base class method called instead of derived class method? 是否可以在同一个 Class(不是派生类)中覆盖基础 Class 中的方法? - Is it possible to override a Method in a Base Class within the same Class (NOT a Derived Class)? 当从基类派生类的对象上调用时,“ this”关键字类型 - “this” keyword type when called on an object of derived class from base class 是否可以要求派生 class 从基 class 实现虚方法? - Is it possible to require a derived class to implement a virtual method from base class? 在foreach循环中使用基类时调用派生类方法 - Call derived class method when using base class in foreach loop 当参数类型是基类时,将Derived类作为参数传递给方法 - Passing Derived class as a parameter to a method when the parameter type is base class 从基类调用时,GetType() 会返回派生最多的类型吗? - Will GetType() return the most derived type when called from the base class? 是否可以在不修改基类的情况下从派生类中更改基类方法中使用的类型? - Is it possible to change the type used in a method of a base class from within a derived class without modifying the base class? 在基本方法中使用派生类 - Using Derived Class In a Base Method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM