你什么时候应该重写方法调用base.Method(),当你在团队编写代码如何标记呢? [英] When should you call base.Method() in overridden method, and how to mark this when you write code in team?

查看:226
本文介绍了你什么时候应该重写方法调用base.Method(),当你在团队编写代码如何标记呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用某些框架/ API,有时是很清楚,如果你必须调用base.Method如果你覆盖它,例如,你可以相当肯定,你应该叫base.Maethod()当你压倒一切的大事invocater,以传播的情况下,在其他情况下,尤其可以在没有源代码可用,并且没有意见不那么清晰。

When using some framework/api, sometimes it's pretty unclear if you must call base.Method if you override it, for example you can be pretty sure that you should call base.Maethod() when you are overriding event invocater, to propagate the event, in other situations it can be not so clear especially when there is no source code available, and no comments.

我wounder程序员如何等决定他们应该叫在这种情况下的基本方法还是不行,如果你是要写一些框架如何通知你期望基地其他程序员方法在虚拟的成员被称为与否。

I wounder how other programmers decide should they call base method or not in this situation, and if you are about to write some framework how to inform other programmers that you expect base method to be called or not in virtual members.

推荐答案

现在我不认为消费者覆盖方法的类应该永远需要调用base.Method()。该代码应该写在这种方式,它不能被打破。

Nowadays I don't think that consumers of a class that override a method should ever need to call base.Method(). The code should be written in such way that it cannot be broken.

public class MyBase
{
    private void FooInternal()
    {
        DoRequiredStuff();
        Foo();
    }
    public virtual void Foo() {}
}

这篇关于你什么时候应该重写方法调用base.Method(),当你在团队编写代码如何标记呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆